Changes

Jump to: navigation, search

C Sharp Variables and Constants

6 bytes added, 14:40, 1 April 2009
no edit summary
== What is a C# Variable? ==
Variables are essentially locations in computer memory that are reserved for storing the data used by an application. Each variable is given 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 of the variable, or changing the value. It is, of course, the ability to change the value of variables which 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.
</pre>
Note that a constant, unlike a variable, must be initialized at the point that it is declared. For example, the following code:
<pre>
</pre>
The above code 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# variables and constants it is time to start looking at the different ''types'' that are available to us as C# programmers.
== C# Integer Variable Types ==
== 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 integers 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<sup>-28</sup> all the way up to 10<sup>28</sup> 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:
== 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:
<pre>

Navigation menu