Introduction to the Values of a Program
Introduction to the Values of a Program
Introduction to Variables
Introduction to Storage
Heap: https://msdn.microsoft.com/en-us/library/ms810466.aspx
A computer is an electronic device that is used to solve one specific problem or to perform an assignment. For example, a fitness machine allows a person to get in shape. A digital camera is used to take pictures. A cell phone is used to make phone calls. An ultrasound machine is a health care machine used to produce images from inside a human body. A music synthesizer can be used to create music:
|
|
An electronic device can also be used to solve general types of problems. A computer can also get its values from a combination of sources:
To manage these different connections, a computer uses motherboard. The calculations; it is called a processor, and it is connected to the motherboard:
The values a computer receives are stored in the computer memory. The computer uses two categories of storage (memory): temporary and permanent. The computer's temporary memory is used to hold information for a while and then lose it if that information is not needed anymore. For example, it can hold some values while the computer is turned on. If the computer gets turned off, the values stored in the temporary memory are lost. This is not an anomaly: that's the way it is designed (it would be catastrophic otherwise; try to imagine a computer that would keep everything; it would be filled in a short time and blow up :)).
The memory area where the computer temporarily stores some values is called random access memory or RAM:
Let's illustrate memory as a small cake pan made of boxes or holes that can contain something:
The RAM is huge, and your program is not the only one that uses the computer. For example, when the computer starts, it launches the operating system (OS). Other programs (and utilities) also occupy the RAM. When a program starts, the compiler reserves a certain area of memory:
A variable is, or indicates, the amount of memory you want a value of your program to use.
Introduction to Numeric Representations
A computer program is a series of instructions that tell the computer what to do, when to do something, and how to do it. To understand the instructions it receives, the computer uses its own language (called the machine language) that uses a combination of 1s and 0s. Because that language is difficult to read (for us regular humans), the computer uses an intermediary language (or program) called an assembler that is closer to (or is in fact) English. The assembler transforms the instructions into a shorter version that uses new words but that are still in English.
The simplified language the computer understands uses a combination of 1s and 0s. This means that the assembler must bring your C# instructions to 1s and 0s. The combinations or sequences of 1s and 0s indicate what needs to be done.
Introduction to the Decimal System
We are familiar with the system that uses 10 digits as 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. A combination of these 10 digits can produce any number. Because this system uses 10 digits. It is named the decimal system.
Introduction to the Binary System
As mentioned already, the language of the computer is made of combinations of two values: 1 and 0. This is called the binary system (because it is based on two values only). Examples of numbers in the binary system, or binary numbers, are 1, 10, 1001, or 1010110111. When you submit a value to a computer, the value must be changed into a combination of 1s and 0s.
Introduction to the Hexadecimal System
An alternative to represent a large number uses the 10 digits of the decimal system and uses the first 6 letters of the English alphabet: A, B, C, D, E, and F or a, b, c, d, e, and f. This means that this system uses a combination of 16 characters to create any number. For this reason, it is called the hexadecimal system. To differentiate a hexadecimal number from a word, the number must start with 0x. Examples are 0x2, or 0xED8, or 0x962AAED3.
One of the characteristics of a number is to indicate whether it is lower than 0, equal to 0, or higher than 0. A number that is lower than 0 is said to be negative. Such a number must be preceded by -. If a number is higher than 0, it is said to be positive. Such a number can be preceded by + or no sign. The ability for a number to hold a sign makes it possible to say that such a number is signed. It is important to know that, when a number is said to be signed, that number can be negative or positive.
Any number that uses neither - or + is said to be positive and, because it doesn't use a sign, it is said to be unsigned. Normally, the 0 number doesn't use a sign.
Variable Declaration
Representing Memory Bits
We mentioned that, to store a value in the computer memory, you must indicate how much memory you will need and you must give it a name. The amount of memory you need is referred to as a data type. As you can imagine, different values require different amount of memory. We also mentioned that the computer considers a value as a combination of 1s and 0s. The area of memory that holds a single piece of 1 or 0 is called a bit.
Consider a bit like a small object (like a cup of a cup cake) that can be full or can be empty:
When it is empty, a bit holds a value of 0. When it is full, it holds a value of 1:
Different combinations of empty (0) and full (1) objects produce the necessary values.
Declaring a Variable
To use a combination of bits, you let the compiler know that you want a variable. This is referred to as declaring a variable. The amount of memory you need is called a data type.
To actually declare a variable, you have various options. If you know the type of value you want to store in memory, use the following formula:
data-type variable-name;
Remember (from previous lessons) that, when you are declaring a variable, you are not required to initialize it. Otherwise, if you know the initial value you want the variable to hold, you can declare it using the var keyword. In that case, you must initialize the variable.
Besides var, if you want to use a variable without specifying the explicit type for the value, you can declare it using the object type or the dynamic keyword.
Initializing a Variable
The Nullity of a Variable
When you declare a variable, you ask the compiler to reserve a certain area and amount of memory to eventually hold a value for that variable. At that time, no clear value is stored in that reserved area of memory. Instead, the area gets filled with garbage (with some languages, such as C/C++, you can find out what that garbage is; some other languages, such as C#, don't allow you to access that garbage; after all, it is garbage and it is useless). A value is referred to as null when it cannot be clearly determined. A null value is not 0 because 0 is an actual value.
In C#, normally, not all variables can hold null values. When declaring a variable, to indicate that it can hold either an actual value or it can be null, after its data type, add the question mark. This can be done as follows:
data-type? variable-name;
Assigning a Value to a Variable
Initializing a variable consists of storing an initial value in that reserved area. The technique you use is the same for all types but the type of value you want to store depends: you should not and must not try to store just any type of value.
When you declare a variable and initialize it, the compiler stores its value in the memory that was reserved for it. You can then access that value when you want. And of course, you can change it if necessary.
As mentioned already, when declaring a variable, you can indicate that it can hold a null value by adding a question mark to its data type. To let you initialize the variable, the C# language provides the null keyword. To initialize a variable with it, simply assign null to it:
class Exercise { static void Main() { data-type? VariableName = null; // You can use the variable } }
class Exercise { static void Main() { data-type? VariableName; variable-name = null; // You can use the variable } }
The rule is that you must assign the value before the variable can be used.
A Byte
A Combination of Four Bits
Although there are cases where you can use or access a bit, you cannot actually store a value in it. That is, you cannot ask the compiler to store the value 1 in a certain bit for you. This is because even the simplest value in C# needs more than one bit to store itself. Still, the minimum combination of bits you can be concerned with is four (in some languages, or in some implementations of the Assembly language, a combination of four bits is called a nibble).
By creating different combinations of empty (0) and full (1) objects grouped in four, you can get 16 combinations:
In a combination of four bits, the bits are counted as 0, 1, 2, and 3. The bit on the most right side is Bit 0 and it is called the low order bit (or LOBIT):
The last bit, the bit on the most left side, is Bit 3 and it is called the high order bit (or HIBIT).
If you represent the four-combination bits in binary formats, you get 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, which produces 16 combinations. In decimal format, these combinations produce 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. In hexadecimal format, these combinations produce 0x0 (0x0000), 0x1 (or 0x0001), 0x2 (or 0x0002), 0x3 (or 0x0003), 0x4 (or 0x0004), 0x5 (0x0005), 0x6 (0x0006), 0x7 (or 0x0007), 0x8 (or 0x0008), 0x9 (or 0x0009), 0xA (or 0xa), 0xB (or 0xb), 0xC (or 0xc), 0xD (or 0xd), 0xE (or 0xe), and 0xF (or 0xF).
As a result, we get a table as follows:
Decimal | Binary | Hexadecimal |
0 | 0000 | 0x0 |
1 | 0001 | 0x1 |
2 | 0010 | 0x2 |
3 | 0011 | 0x3 |
4 | 0100 | 0x4 |
5 | 0101 | 0x5 |
6 | 0110 | 0x6 |
7 | 0111 | 0x7 |
8 | 1000 | 0x8 |
9 | 1001 | 0x9 |
10 | 1010 | 0xA |
11 | 1011 | 0xB |
12 | 1100 | 0xC |
13 | 1101 | 0xD |
14 | 1110 | 0xE |
15 | 1111 | 0xF |
Table of Numeric Conversions
The minimum and maximum values in a combination of four bits are:
Decimal | Hexadecimal | Binary | |
Minimum | 0 | 0x0 | 0000 |
Maximum | 15 | 0xf | 1111 |
You will never be concerned with combinations of four bits because you cannot store anything in it (these combinations are too small).
A Combination of 8 Bits
We presented them here for two reasons. First, you should be aware of such a thing as a combination of four bits so you will know where the number 16 comes from when you see it mentioned in different places. Second, it allowed us the introduce the byte.A byte is a combination of eight adjacent bits. The first bit, located on the most right side, is Bit 0. The last bit, on the most left side, is Bit 7:
Bit 0 is called the least significant bit. It is also called the low order bit or LOBIT. Bit 7 is called the most significant bit. It is also called the high order bit or HIBIT.
If you create different combinations of empty (0) and full (1) objects grouped in eight, in binary formats, you get 00000000, 00000001, 00000010, 00000011, 00000100, 00000101, 00000110, 00000111, 00001000, 00001001, 00001010, 00001011, 00001100, 00001101, 00001110, 00001111, etc, up to 11111111. When a large number is represented in binary format, it can be difficult to read. An alternative is to create groups of four bits so that instead of writing 01001011, you would write 0100 1011.
To evaluate the number of combinations in decimal format, you use the number 2 that represents decimal, you elevate it to the power of the position of the bit (0, 1, 2, 3, 4, 5, 6, or 7), and you add the number. This can be done as follows:
27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
Therefore, we get 255 possible combinations of eight bits. The combinations can also be represented in hexadecimal format as 0x1, 0x2, ..., 0xA, 0xA1, ..., 0xC8, ..., up to 0xFFFF.
In the three numeric systems, we get:
Decimal | Hexadecimal | Binary | |
Minimum | 0 | 0x0 | 0000 |
Maximum | 255 | 0xff | 1111 1111 |
The minimum storage area offered by most (the majority of) processors is the byte. As you know already, a byte is a group of 8 consecutive bits. The amount of memory space offered by a byte can be used to store just a single symbol, such as those you see on your keyboard. These symbols, also called characters, have been organized by the American Standard Code for Information Exchange (ASCII) in a list. But, ASCII uses only 128 decimal numbers (based on a 7-bit format) to represent symbols counted from 0 to 127. To compensate for the remaining 1 bit, IBM used it to organize special characters, foreign language characters, mathematical symbols, small graphics, etc. Each one of these characters has a decimal, a hexadecimal, and a binary equivalent.
To let you declare a variable for a small number, the C# language provides a data type named byte. You can use it to declare a variable for a small number. Here is an example:
using System;
class Program
{
static void Main()
{
byte small;
byte other;
}
}
A byte is an unsigned number whose value can range from 0 to 255. Therefore, when assigning a value to a byte variable, the value nust fit in only one byte. To display the value to the console, you can pass its variable to the Console.Write() or the Console.WriteLine() method. Here are examples:
using System; class Program { static void Main() { byte small = 150; byte other = 250; Console.Write("Number: "); Console.WriteLine(small); Console.Write("Number: "); Console.WriteLine(other); Console.WriteLine("=================="); } }
This would produce:
umber: 150 umber: 250 ================= ress any key to continue . . .
When assigning a value to a byte variable, make sure the value is between 0 and 255. If the value is less than 0 or higher than 255, when you trying using the variable, you will receive an error or the compiler will throw an exeption.
Instead of a decimal number, you can also initialize a byte variable with a hexadecimal value. The value must be between 0x00 and 0xFF (255). Here is an example:
class Exercise
{
static void Main()
{
var number = 0xFE;
System.Console.Write("Number: ");
System.Console.WriteLine(number);
}
}
This would produce:
Number: 254 Press any key to continue . . .
A byte number is referred to as signed if it can hold a negative or a positive value that ranges from -128 to 127. To support signed-byte numbers, the C# language provides a data type named sbyte. You can use it to declare a variable that would hold small positive or negative numbers. Here is an example:
class Exercise
{
static void Main()
{
sbyte roomTemperature = -88;
System.Console.Write("When we entered, the room temperature was ");
System.Console.WriteLine(roomTemperature);
}
}
This would produce:
When we entered, the room temperature was -88
Introduction to the Characteristics of Data Types
Introduction
To assist you in creating and managing values for your applications, the .NET Framework provides various structures. There is a particular structure for each of the types of values we will study. All those structures were created in the System namespace. As a result, you don't have to import any special library to use those types. As long as you include that namespace in a document, you are ready. When it becomes necessary, we will give the name of the structure for each type.
As you will see, some of the names of the C# data type will be different from the equivalent .NET Framework structures. Nevertheless, each data type has some characteristics (such as the limits of a type). The characteristics of a type are controlled by its equivalent .NET Framework structure.
To support small numbers that fit in 8 bits, the .NET Framework provides a structure named Byte. You can use it to declare a variable. A Byte data type primarily follows the same rules as the byte type.
Converting a Value to a String
In most cases, to display the value of a variable to a console, we already know that you can simply pass the name of the variable to the Console.Write() or the Console.WriteLine() method. Here is an example:
using System; class Program { static void Main() { byte small = 150; Byte other = 250; Console.Write("Number: "); Console.WriteLine(small); Console.Write("Number: "); Console.WriteLine(other); Console.WriteLine("=================="); } }
To display the value of a variable to a Windows control, you may have to convert the variable to a string. To assist you with this, each structure of a data type is equipped with an overloaded method named ToString. The simplest version of this method doesn't take any argument. Its syntax is:
public string ToString ();
To use this version, call it on the variable (we have already seen hundreds of examples and we will see even more).
To let you control how a value should display, each structure of a data type is equipped with another version of the ToString() method whose syntax is:
public string ToString (string? format);
This version takes a string as argument. The argument uses a letter as follows:
Use the Letter | To Display the Value as |
C | Currency |
E | Exponent |
F | Fixed |
G | General |
N | Natural |
P | Percentage |
X | Hexadecimal |
The Minimum Value of a Type
Every data type has limits. Those limits specify the lowest and the highest value the variable of a type can hold. To provide the lowest value that a type can hold, each structure is equipped with a constant field named MinValue. For the byte type, this field is defined as follows:
public const byte MinValue = 0;
To know the lowest value of a type, you can access this field. Here is an example:
using System; using System.Windows.Forms; namespace ValuesCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnShow_Click(object sender, EventArgs e) { txtByteMinValue.Text = byte.MinValue.ToString(); txtByteMinimumValue.Text = Byte.MinValue.ToString(); } } }
The Maximum Value of a Type
Every data type has a highest value beyond which it is not valid. To provide that value, each structure is equipped with a constant field named MaxValue. For the byte type, this field is defined as follows:
public const byte MaxValue = 255;
To get the highest value of a type, access this field. Here is an example:
using System; using System.Windows.Forms; namespace ValuesCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnShow_Click(object sender, EventArgs e) { txtByteMinValue.Text = byte.MinValue.ToString(); txtByteMinimumValue.Text = Byte.MinValue.ToString(); txtByteMaxValue.Text = byte.MaxValue.ToString(); txtByteMaximumValue.Text = Byte.MaxValue.ToString(); } } }
Parsing a Value
As mentioned in previous lessons, the values that a user enters in Windows controls are considered strings. If you want to involve a provided value to numeric expressions, you should first convert the value to the appropriate type. To assist you with that operation, each structure of a data type is equipped with an overloaded static method named Parse. One of the versions of this method uses the following syntax:
public static byte Parse (string s);
As we have been doing so far, to call this method, write the data type, a period, and Parse(). In the parentheses of the Parse() method, enter the value to convert.
Trying to Parse a String
Sometimes, the Parse() method fails. For example, if you don't provide any value (in which case the value would be considered null) or you provide a value that is in a bad format (such as "124;85"), the (above version of the) Parse() method is not equipped to convert such a value. As an alternative, each structure of a data type is equipped with an overloaded static method named TryParse. The most fundamental version of this method uses the following syntax:
public static bool TryParse (string s, out byte result);
This method takes two arguments. The first argument is the value to convert. it's the same type of value you would pass to a Parse() method. The second argument is passed by reference. Since this argument is passed as an out reference, first declare a variable and pass it as the second argument (we will see examples in other sections).
Once again, remember that when a user provides a value in an application, by default, that value is a string and cannot be directly used in a numeric operation. To assist you with value conversion, the .NET Framework provides a static class named Convert:
public static class Convert
The Convert class is equipped with an overloaded method for each of the data type we will review. The methods associated with a data type are used to convert a value from a certain type to that type. Based on this, the method used to convert a value to a small nurnber is named ToByte. Remember that the method is overloaded. The version used to convert a string, such as a value from a Windows control, to a byte uses the following syntax:
public static byte ToByte (string value);
The version used to convert any value to a byte uses the following syntax:
public static byte ToByte (object value);
In both cases, pass the desired value to the method.
The C# language provides the unary sizeof operator that allows you to find out how much space a data type uses. To find out how much memory space a data type uses, type it between the parentheses of the sizeof operator. Here is an example:
The byte data type uses sizeof(byte) bytes.
Introduction
A group of 16 consecutive bits is called a word. The bits are counted from right to left starting at 0:
Considered as a group of 16 bits, the most right bit of a word, bit 0, is called the least significant bit or low order bit or LO bit or LOBIT. The most left bit, bit 15, is called the most significant bit or high order bit or HI bit or HIBIT. The other bits are referred to using their positions: bit 1, bit 2, bit 3, etc.
Considering that a word is made of two bytes, the group of the right 8 bits is called the least significant byte or low order byte or LO byte or LOBYTE. The other group is called the most significant byte or high order byte or HI byte or HIBYTE.
The representation of a word in binary format is 0000000000000000. To make it easier to read, you can group bits by 4, like this: 0000 0000 0000 0000. Therefore, the minimum binary value represented by a word is 0000 0000 0000 0000. The minimum decimal value of a word is 0. The minimum hexadecimal value you can store in a word is 0x0000000000000000. This is also represented as 0x00000000, or 0x0000, or 0x0. All these numbers produce the same value, which is 0x0.
The maximum binary value represented by a word is 1111 1111 1111 1111. To find out the maximum decimal value of a word, you can use the base 2 formula, filling out each bit with 1:
1*215+1*214+1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20
= 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
= 65535
To find out the maximum hexadecimal number you can store in a word, replace every group of 4 bits with an f or F:
1111 | 1111 | 1111 | 1111 | ||||
f | f | f | f | ||||
|
A word, which is a group of 16 contiguous bits or 2 bytes, can be used to hold a natural number. As we have studied, the maximum numeric value that can fit in a word is 65535. To declare a variable for such a value, you can use the var keyword and initialize the variable with a value between -32768 et 32767. Here is an example:
class Exercise
{
static void Main()
{
var schoolEffective = 1400; // Number of Students
System.Console.Write("School Effective: ");
System.Console.WriteLine(schoolEffective);
}
}
This would produce:
School Effective: 1400 Press any key to continue . . .
Since the byte is used for characters and very small numbers, whenever you plan to use a number in your program, the minimum representation you should use is a word.
A natural number is also called an integer. To let you declare a variable that can hold an integer that can be stored in a word, the C# language provides a data type named short. A short integer is signed by default. Therefore, its variable can store a value that ranges from -32768 to 32767. Here is an example program that uses a short data type:
using System; using static System.Console; class Program { static void Main() { short numberOfPages; short temperature; numberOfPages = 842; temperature = -1544; Write("Number of Pages of the book: "); WriteLine(numberOfPages); Write("Temperature to reach during the experiment: "); Write(temperature); WriteLine(" degrees\n"); WriteLine("=================="); } }
This would produce:
Number of Pages of the book: 842 Temperature to reach during the experiment: -1544 degrees
Because a short integer handles numbers that are larger than the signed byte, any variable you can declare for a signed byte can also be declared for a short variable.
.NET Short Integers
To support short integers, the .NET Framework provides a structure named Int16. This structure is equipped with the exact same fields and properties we reviewed for the byte type. These include the minimum and maximum values. Here is an example of accessing them:
using System; using System.Windows.Forms; namespace ValuesCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblByteMinValue.Text = byte.MinValue.ToString(); lblByteMaxValue.Text = Byte.MaxValue.ToString(); lblShortMinValue.Text = Int16.MinValue.ToString(); lblShortMaxValue.Text = short.MaxValue.ToString(); } } }
The Int16 structure is also equipped with the abilities to parse a variable. The Convert is also equipped with the methods necessary to convert a value to a short integer:
public static short ToInt16 (string value); public static short ToInt16 (object value);
If a variable must hold positive and relatively small numbers, it is referred as an unsigned short integer. To let you declare variables for such numbers, the C# language provides a data type named ushort. You can use it of the var keyword to declare the variable. An unsigned short integer can hold numbers that range from 0 to 65535 and therefore can fit in 16 bits. Here are examples:
class Exercise
{
static void Main()
{
// These variables must hold only positive integers
ushort numberOfTracks;
ushort musicCategory;
numberOfTracks = 16;
musicCategory = 2;
System.Console.Write("This music album contains ");
System.Console.Write(numberOfTracks);
System.Console.WriteLine(" tracks");
System.Console.Write("Music Category: ");
System.Console.Write(musicCategory);
System.Console.WriteLine();
}
}
This would produce:
This music album contains 16 tracks Music Category: 2
When C# was invented, one of its biggest goals was to avoid some of the difficulties of C/C++. Among them was the use of pointers. C/C++ uses pointers to refer to the area in memory where a value is located. C# highly avoids pointers and takes over memory management as opposed to letting the programmer take care of that aspect of an application. You can still use pointers in C# in extreme cases when you judge them necessary.
Because the C# compiler is in charge of managing the memory used by the values of an application, pointers are said to be unsafe. If you want to use a pointer in your application, you must precede the name of every method that uses unsafe code with the unsafe keyword. Here is an example:
class Exercise
{
unsafe static void Main()
{
int Length = 224;
int *Len = &Length;
System.Console.Write("Length ");
System.Console.WriteLine(Length);
System.Console.Write("Length ");
System.Console.WriteLine(*Len);
System.Console.WriteLine();
Length = 804;
System.Console.Write("Length ");
System.Console.WriteLine(Length);
System.Console.Write("Length ");
System.Console.WriteLine(*Len);
}
}
To compile the application, you must indicate that you are using unsafe code. To do that, use the /unsafe modifier. Here is an example:
csc /unsafe Exercise.cs
To apply this option in Microsoft Visual Studio, on the main menu, you can click Project -> Project Properties... In the Build section, click the Allow Unsafe Code check box:
|
||
Home | Copyright © 2008-2021, FunctionX | Next |
|