Difference between revisions of "C Sharp Variables and Constants"
(→What is a C# Constant?) |
(→C# Integer Variable Types) |
||
Line 61: | Line 61: | ||
<td>byte<td>1 byte<td>0 to 255</td> | <td>byte<td>1 byte<td>0 to 255</td> | ||
<tr> | <tr> | ||
− | <td>sbyte<td>1 byte<td>-128 | + | <td>sbyte<td>1 byte<td>-128 to 127</td> |
<tr> | <tr> | ||
− | <td>short<td>2 bytes<td>-32,768 | + | <td>short<td>2 bytes<td>-32,768 to 32,767</td> |
<tr> | <tr> | ||
− | <td>ushort<td>2 bytes<td>0 | + | <td>ushort<td>2 bytes<td>0 to 65,535</td> |
<tr> | <tr> | ||
− | <td>int<td>4 bytes<td>-2,147,483,648 | + | <td>int<td>4 bytes<td>-2,147,483,648 to 2,147,483,648</td> |
<tr> | <tr> | ||
− | <td>uint<td>4 bytes<td>0 | + | <td>uint<td>4 bytes<td>0 to 4,294,967,295</td> |
<tr> | <tr> | ||
− | + | <td>long<td>8 bytes<td>-10<sup>20</sup></td> | |
</table> | </table> |
Revision as of 18:32, 11 January 2008
It is impossible gain proficiency in any programming language without first learning some the basics. Perhaps the most basic aspect of programming involves the use of variables and constants. Even the most advanced and impressive programs from high end video games to enterprise commerce applications use variables in one form or another. In this chapter of C# Essentials we will cover everything that a C# programmer needs to know about variables and constants.
What is a C# Variable?
Variables are essentially locations in computer memory that is reserved for storing the data used by an application. Each variable is assigned a name by the programmer and assigned a value. The name assigned to the variable may then be used in the C# code to access the value assigned to the variable. This access can involve either reading the value the variable, or changing the value. It is, of course, ability to change the value of a variable that gives them the name variable.
A variable must be declared as a particular type such as an integer, a character or a string. C# is what is known as a strongly typed language in that once a variable has been declared as a particular type it cannot subsequently be changed to a different type. While this may come as a shock to those familiar with loosely typed languages such as Ruby it will be familiar to Java, C and C++ programmers. Whilst it is not possible to change the type of a variable it is possible to disguise the variable as another type under certain circumstances. This involves a concept known as casting and will be covered later in this chapter.
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?
A constant is similar to a variable in that it provide a named location in memory to store a data value. Constants differ in one significant way in that once a value has been assigned to constant it cannot subsequently be changed.
Constants are particularly useful if there is a value which is used repeatedly throughout the application code. Rather than use the value each time, it makes the code easier to read if the value is first assigned to a constant and which is then referenced in the code. For example, it might not be clear to someone reading your C# code why you used the value 5 in an expression. If, instead of the value 5, you use a constant named interestRate the purpose of the value becomes much clearer. Constants also have the advantage that the if the programmer needs to change a widely used value it only needs to be changed once in the constant declaration and not each time it is referenced.
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, the follwoing code:
const int interestRate; // Invalid - a const must be initialized at creation time
will result in a compilation error along the lines of A const field requires a value to be provided. Now that we have described C# variable and constants it is time to start looking at the different types.
C# Integer Variable Types
Perhaps the most widely used of the variable types is the integer. C# provides a number of different integer types based on number size and whether the integers are signed (positive only) or unsigned (positive or negative). All the integer variable types have one thing in common and that is that they may only be used to store whole numbers.
The following table lists the various C# integer variable types together details of the number of bytes of physical memory consumed by each type and the acceptable value ranges.
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 |