Data Types
The major data types in C# are shown in Table 1.
|
Bool |
true or false |
|
Byte |
unsigned 8-bit value |
|
short |
16-bit integer |
|
int |
32-bit integer |
|
long |
64-bit integer |
|
float |
32-bit floating point |
|
double |
64-bit floating point |
|
char |
16-bit character |
|
string |
16-bit characters |
Table 1 - Data types in C#
Note that the lengths of these basic types are irrespective of the computer type or operating system. Characters and strings in C# are always 16 bits wide: to allow for representation of characters in non-Latin languages. It uses a character coding system called Unicode, in which thousands of characters for most major written languages have been defined. You can convert between variable types in the usual simple ways:
· Any wider data type can have a narrower data type (having fewer bytes) assigned directly to it, and the promotion to the new type will occur automatically. If y is of type float and j is of type int, then you can write:
float y = 7.0f; //y is of type float
int j; //j is of type int
y = j; //convert int to float
to promote an integer to a float.
· You can reduce a wider type (more bytes) to a narrower type by casting it. You do this by putting the data type name in parentheses and putting it in front of the value you wish to convert:
j = (int)y; //convert float to integer
You can also write legal statements that contain casts that might fail, such as
float x = 1.0E45;
int k = (int) x;
If the cast fails, an exception error will occur when the program is executed. Boolean variables can only take on the values represented by the reserved words true and false. Boolean variables also commonly receive values as a result of comparisons and other logical operations:
int k;
boolean gtnum;
gtnum = (k > 6); //true if k is greater than 6
Unlike C or C++, you cannot assign numeric values to a Boolean variable and you cannot convert between Boolean and any other type.
Converting Between Numbers and Strings
To make a string from a number or a number from a string, yo u can use the Convert methods. You can usually find the right one by simply typing Convert and a dot in the development enviroment, and the system will provide you with a list of likely methods.
string s = Convert.ToString (x);
float y = Convert.ToSingle (s);
Note that “Single” means a single-precision floating point number.
Numeric objects also provide various kinds of formatting options to specify the number of decimal places:
float x = 12.341514325f;
string s =x.ToString ("###.###"); //gives 12.342
Declaring Multiple Variables
You should note that in C#, you can declare a number of variables of the same type in a single statement:
int i, j;
float x, y, z;
This is unlike VB6, where you had to specify the type of each variable as you declare it:
Dim i As Integer, j As Integer
Dim x As Single, y As Single, z As Single
Numeric Constants
Any number you type into your program is automatically of type int if it has no fractional part or type double if it does. If you want to indicate that it is a different type, you can use various suffix and prefix characters:
float loan = 1.23f; //float
long pig = 45L; //long
int color = 0x12345; //hexadecimal
C# also has three reserved word constants: true, false, and null, where null means an object variable that does not yet refer to any object. We’ll learn more about objects in the following chapters
Character Constants
You can represent individual characters by enclosing them in single quotes:
char c = ‘q’;
C# follows the C convention that the white space characters (non printing characters that cause the printing position to change) can be represented by preceding special characters with a backslash, as shown in Table 2. Since the backslash itself is thus a special character, it can be represented by using a double backslash

Join the conversation! Your thoughts help the community grow.