C#.NET
C# & C#.NET info
What is a C# Variable?
Variable declarations require a type, a name and, optionally a value assignment. The following example declares an integer variable called interestRate but does not initialize it:
int interestRate;
The following example declares and initializes a variable using the assignment operator (=):
int interestRate = 10;
A new value may be assigned to a variable at any point after it has been declared.
int interestRate = 5; //Declare the variable and initialize it to 5 interestRate = 10; // variable now equals 10 interestRate = 20; // variable now equals 20
What is a C# constant?
As with variables, constants have a type, a name and a value. Unlike variables, constants must be initialized at the same time that they are declared and must be prefixed with the const keyword:const int interestRate = 10;
Note that a constant, unlike a variable, must be initialized at the point that it is declared. For example:
const int interestRate; // Invalid - a const must be initialized at creation time
C# Integer Variable Types
Type | Size in Bytes | Value Range |
---|---|---|
byte | 1 byte | 0 to 255 |
sbyte | 1 byte | -128 to 127 |
short | 2 bytes | -32,768 to 32,767 |
ushort | 2 bytes | 0 to 65,535 |
int | 4 bytes | -2,147,483,648 to 2,147,483,648 |
uint | 4 bytes | 0 to 4,294,967,295 |
long | 8 bytes | -1020 to 1020 |
ulong | 8 bytes | 0 to 2 x 1020 |
C# Floating Point Variables
Integers are fine for dealing with whole numbers but of little use when there are numbers after the decimal point. Such numbers may be stored in float or double variable types. The default type for such numbers is double. The following table shows the two types with comparisons of the number ranges supported and the number of significant digits in each case:
Type | Size in Bytes | Value Range | Digit Accuracy |
---|---|---|---|
float | 4 bytes | 1.5 * 10-45 to 3.4 * 1038 | 6 - 7 digits |
double | 8 bytes | 5.0 * 10-324 to 1.7 * 10308 | 15 - 16 digits |
The C# Decimal Variable Type
Both the integer and floating point families of C# variable types have some limitations. Integers can only handle whole numbers, resulting in the fractional part of a value being stripped off. Floats, on the other hand, have problems with rounding accuracy. Clearly the best of both worlds is sometimes needed and to address this requirement the decimal variable type is provided. The decimal type is a compromise between integer and float variable types in that it can store fractional parts of a value and provide exact values in computations. The decimal variable type is capable of holding values in the range 10-28 all the way up to 1028 with none of the rounding problems associated with floating point variable types.
C# Boolean Variable Type
The C# Boolean variable type is declared using the bool keyword and allows for the storage of true and false values. Boolean variables are particularly useful in flow control constructs such as if and while statements. Unlike some other programming languages, C# boolean variables must be assigned either true or false and cannot be assigned 1 or 0:
bool loopFinished = false; loopFinished = true;
C# Character Variable Type
When we talk about characters we are referring to individual letters and numbers. For example, the letter 'a' is a character, as is the visual representation of the number '1'. Such characters may be stored in a C# char variable. A char variable can contain one character and one character only. It is important to be aware that a character is not limited to those in the English alphabet. A character stored in a char variable can be Chinese, Japanese, Arabic, Cyrillic, Hebrew or any other type of character you care to mention. In addition, characters can be used for counting in loops and even in mathematical expressions. To assign a character to a variable simply surround the character with singe quotes: char myLetter = 'a'; C# provides a number of special character constants which have a special meaning when displayed. These special constants perform such tasks as displaying tab and new lines in text. The following table lists the characters, all of which are identified by a preceding backslash (\):
Character Constant | Special Value |
---|---|
\n | New Line |
\t | Tab |
\0 | Null |
\r | Carriage Return |
\\ | Backslash |
C# String Variables
In the preceding section we looked at storing individual characters in a char variable. Whilst this works for storing a single letter or number it is of little use for storing entire words or sentences. For this purpose the string variable type is supported by C#. Variables of type string can store a string of any number of characters. String values are surrounded by double quotes ("). For example: string myString = "This is a string"; A string declaration in C# cannot be spread over multiple lines. If a string needs to be split over multiple lines by new lines the \n special character can be used. For example:
string myString = "This is line one\nThis is line two\nThis is line 3"; System.Console.WriteLine (myString);
Casting Variable Types in C#
In instances where it is safe to do so without data loss, C# will allow you to assign a value from one type of variable to another simply using the assignment operator. For example, since a long variable is quite capable of storing any value that can be stored in an int variable an assignment such as the following is perfectly valid:
int myInteger = 20; long myLong; myLong = myInteger;
The same is not true of assigning a long to an int since a long is capable of storing significantly larger numbers than an int (an int is 4 bytes long versus 8 bytes for a long). Attempting to squeeze a long into an int value would inevitably result in lost data. For this reason the C# compiler will flag such an attempt as an error and stubbornly refuse to compile the code. It is just possible, however, that you as the programmer know that even though a variable is a long type that it will never contain a value greater than an int is capable of storing. In this case you might legitimately want to assign the value stored in a long variable to an int variable. This can be achieved by using a cast to convert the long value. Casts are performed by placing the variable type to which you wish to convert before the name of the variable you wish to convert from. For example, to convert a long value to an int during an assignment operation:
int myInteger; long myLong = 1232; myInteger = (int)myLong;
Note that casting is only possible on numerical data types. It is not, therefore, possible to perform casts on string, or bool variables.
C# comments
Use a double slash to comment out a line.
// This line is commented out in C#
Sources:
http://www.techotopia.com/index.php/C_Sharp_Variables_and_Constants
http://www.homeandlearn.co.uk/csharp/csharp.html