Sponsored Ad

Thursday, October 22, 2009

C# Variables

Declare variables in C# using the following syntax:

datatype identifier;
For example:
int i;

This statement declares an int named i . The compiler won ’ t actually let you use this variable in an expression until you have initialized it with a value.

Once it has been declared, you can assign a value to a variable using the assignment operator =:

i = 10;
You can also declare the variable and initialize its value at the same time:
int i = 10;

This syntax is identical to that of C + + and Java syntax, but very different from the Visual Basic syntax for declaring
variables. If coming from Visual Basic 6, must also take into account that C # did not distinguish
between objects and simple types, so there is no need for anything like the Set keyword, even if
want your variable to refer to an object. The C # syntax for declaring variables is the same

If you declare and initialize over five variable in a single statement, all of the variables will be of
the same data type:

int x = 10, y =20;                            // x and y are both ints

To declare variables of different types, you need to use separate statements. You cannot assign different
data types within a multiple variable declaration:

int x = 10;
bool y = true;                                       // Creates a variable that stores true or false
int x = 10, bool y = true;                   // This won’t compile!

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers