C# Variables


  • A Variable is a name given to storage area of any value in the program.
  • Variable's name is used to refer the data value stored at particular memory location.
  • The value stored in any variable can be changed at the run time.

Naming convention of a Variable in C#


There are various rules which we have to know for naming a variable in C#. These rules are as given below:


  • The name of variable consists of the alphabets, digits (0-9) and underscores character (_).
  • They must not begin with the digits.
  • It cannot contains the characters like {,(,},),!,#,$,%,^,&,*,?,/,\ etc.
  • Uppercase letter and lowercase are treated distinctly at the time of naming variable because C# is a case sensitive language.
  • Keyword cannot be a name of variable. If we want to use the keyword as the name of variable then we have to use it with prefix "@".
  • Whitespaces are not allowed in the variable name.
  • Variable name can be of any length.

Here is a table which tells us about the valid or invalid variable's name:


Variable Name Status
Name

Valid

@if valid
If invalid
8age invalid
Student^age invalid
Student_name valid

Variable Declaration in C#


  • A variable name must be declared before using it into the program.
  • It can be declare with a meaningful name of a specific data type.
  • Here is the syntax for the variable declaration:
  • datatype variable;

  • We can declare number of variables of same data type in a single line as given below:
  • datatype variable1, variable2, variable3...................variableN;

Initialization of Variables


  • After declaration of variable you need to initialize the variable.
  • It is not mandatory to initialize the variable at the time of declaration it is totally depend upon the choice of the user.
  • Variable must be initialized before its use in the program.
  • Here is the syntax for the initialization of a variable:
  • datatype variable1=value,
    or
    datatype variable1;
    variable1=value;

  • Here are some examples of initialization of the variable:
  • int length = 5;
    float value= 100.00f;
    char c1= 'a';

Scope of Variables


  • Scope of variable defined the accessed area of any variable in the program.
  • On the basic of scope a variable can be categorized into two parts:
    • Local Variable
    • Static Variable
Local Variables

  • Local Variables are those variables whose scope is within the method where they have been declared.
  • Here is an example which shows the Local Variables:
  • Class First
    {
    public void method1()
    {
    int x=5; //Local Variable
    int y=6; // Local Variable
    ........................................
    ........................................
    }
    }

Static Variable

  • Static Variables are those variables which retain their value throughout the program.
  • We can access the static variable with the class name.
  • Here is an example which shows the static Variable:
  • Class First
    {
    static int x=5;// Static variable
    public void method1()
    {
    Console.WriteLine(First.x);
    .............................................
    .............................................
    }
    }

In the above code x is a static type integer variable that can be used throughout the program with the class name.


Example

Here is an example which shows the declaration and initialization of variables of different-different data types:


using System;
namespace VariableExample
{
class First
{
static void Main()
{
/* Variable Declaration*/
int x;
char c;
double d;
/*Variable Initialization*/
x=12;
c='h';
d=10.00;
Console.WriteLine("x={0},c={1},d={2}",x,c,d);
Console.ReadLine();
}
}
}

Above program will produce the following output:


Output

x=12,c=h,d=10

Note: In the above program you can see the elements like {0}, {1}, {2}. These are the placeholders that are for displaying value of variable. In the above program {0} displays the value of variable x, {1} displays the value of variable c and {2} displays the value of variable d.