Variable Declaration and Assignment

To recap, recall that you declare variables simply using their type and name:

int age;

You then assign values to variables using the = assignment operator:

age = 25;

Remember that variables must be initialized before you use them. The preceding assignment could be used as an initialization. There are a couple of other things you can do here that you are likely to see in C# code. One, you can declare multiple variables of the same type at the same time by separating their names with commas after the type, as shown here:

int xSize, ySize;

Here, xSize and ySize are both declared as integer types. The second technique you are likely to see is assigning values to variables when you declare them, which basically means combining two lines of code:

int age = 25;

You can use both these techniques together:

int xSize = 4, ySize = 5;

Here, both xSize and ySize are assigned different values. Note that

int xSize, ySize = 5;

results in only ySize being initialized — xSize is just declared, and it still needs to be initialized before it ’ s used.