Changes

Jump to: navigation, search

C Sharp Variables and Constants

615 bytes added, 16:36, 11 January 2008
What is a C# 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:
 
<pre>
int interestRate;
</pre>
 
The following example declares and initializes a variable using the ''assignment operator'' (=):
 
<pre>
int interestRate = 10;
</pre>
 
A new value may be assigned to a variable at any point after it has been declared.
 
<pre>
int interestRate = 5; //Declare the variable and initialize it to 5
 
interestRate = 10; // variable now equals 10
 
interestRate = 20; // variable now equals 20
</pre>
== What is a C# Constant? ==

Navigation menu