Variables and Standard Types

Declaring variables

You declare variables in your code using a declaration statement, which in C#, looks like this:

string name;

This simple example creates a variable, named name, of type string.

using System; public class Program...

 

Only after this declaration has happened can your variable be used in code, if you try and use it before you have declared it, the compiler will give you a warning:

You can also make an assignment as part of the declaration statement, so you can set the initial value of your variable:

string name = "Matt";

 

Page 3 of 17