C# Variable

In the first C# program chapter, we declared a variable called "message" as shown below.

Example: C# Variable
namespace CSharpTutorials
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "Hello World!!";

            Console.WriteLine(message);
        }
    }
}

The variable in C# is nothing but a name given to a data value. In the above example, message is the name of the variable that stores the string data value "Hello World!!". As the name suggests, the contents of a variable can vary, i.e., you can change the value of a variable at any time.

In C#, a variable is always defined with a data type. The following is the syntax variable declaration and initialization.

Variable Syntax:
<data type> <variable name>;

<datatype> <variable name> = <value>;

A variable can be declared and initialized later or it can be declared and initialized at the same time. In the following example, the first statement declares a variable called "message" without assigning any value to it. In the second statement, a value is assigned to the "message" variable.

Example: Variable Declaration
string message;

// value can be assigned after it declared 
message = "Hello World!!";

In the following example, variable is declared and initialized (a value is assigned to it) at the same time.

Example: Variable Declaration & Initialization
string message = "Hello World!!";

Multiple variables of the same data type can be declared and initialized in a single line separated by commas.

Example: Multiple Declarations
int i, j, k, l = 0;

int amount, num;

When declaring multiple variables of the same data type, you can put them in multiple lines for the sake of readability; even if split across multiple lines, the compiler will consider it to be one statement, until it encounters a semicolon (;).

Example: Multi-Line Declarations
int i, j, 
    k, 
    l = 0;

The value of a variable can be assigned to another variable of the same data type. However, a value must be assigned to a variable before using it.

Example: Variable Assignment
int i = 100;

int j = i; // value of j will be 100

The following example would give a compile time error because string value cannot be assinged to a int type variable.

Example: Invalid Variable Assignment
string message = "Hello World!!";

int i = message; // compile time error

You must assign a value to a variable before using it otherwise the compiler will give an error. For example, in the following code, we have declared a variable called i without assigning any value to it. If we then try to display the value of the variable on the console, we will get a compile time error.

Example: Invalid Variable Assignment
int i;

//Following will give compile time error: "Use of unassigned local variable 'i'"
int j = i;
Console.WriteLine(j); 
Points to Remember :
  1. The variable is a name given to a data value.
  2. A variable holds the value of specific data type e.g string, int, float etc.
  3. A variable can be declared and initialized in separate statements and also in the single statement.
  4. The value of a variable can be changed at any time throught out the program as long as it is accessible.
  5. Multiple variables can be defined seperated by comma (,) in a single or multiple line till semicolon(;).
  6. A value must be assigned to a variable before using it otherwise it will give compile time error.