Home > C# > Variables in C#

What is variable?

A variable can be defined as a name given to memory to store some value in it. And a computer program can change these values at any time during its execution depending on the code. So, a variable has a name, it also has a data type which indicates the type of data to be stored in it.

Variable Declaration

When we give a name to a memory space it is called variable declaration. Its basic syntax is:
data-type variable name;
Rules for naming a variable are almost same as for a variable name in C++.
E.g.

int x; // this will store an Integer value
char y; // this will hold a single character value
float z; // this can hold a Floating point value

more than 1 variables of same data type can be created using single statement, each variable name separated by a comma (,).
E.g.

int x, y, z;
char ch1, ch2, ch3;
Variable initialization

When a value is stored for the first time in a memory location it is called variable initialization.
It can be done in two ways:
data-type variable name = variable-value;
E.g.

int x=10; // this shows that 10 is stored to a memory location named x.
data-type variable name;
variable name = variable-value;
int y;
y=20; //this shows that 20 is stored at a memory location named y.

both above-written examples are doing same work i.e. initializing a variable.
More than one variables of same data type can be initialized with the same value using a single statement.
E.g.

int x = y = z = 30; // this will initialize x, y & z with 30.

Below given is a simple program which shows declaration and initialization of variable/s in C#.
Example Program

using System;

namespace variable
{
    class Program
    {
        static void Main(string[] args)
        {
            // declaration
            int x;
            double y;
            char z;

            // initialization 
            x = 10;
            y = 13.9;
            z = 'a';

            // writing these values to console
            Console.Write("Value of x = "+x+ "\nValue of y = " + y + "\nValue of z = " + z+"\n");
        }
    }
}

Output
variable example

Just like C++ variable names in C# are also case sensitive. Let us have a look at below simple example which explains this.
Example Program

using System;

namespace variable
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            double X;
            
            x = 10;
            X = 13.9;
            Console.Write("Value of x = " + x + "\nValue of X = " + X + "\n");
        }
    }
}

Output
case sensitive variables

Scope of Variables in C#

Local

Local variables are the one which is not accessible outside the boundary in which they were created.
E.g.
In the example below all the variables created inside the main function of the class program so we can access them only inside that main function. As shown in below image when they were accessed in another class program1 we get a compile-time error.

using System;
namespace variable
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            double X;
            char z;

            x = 10;
            X = 13.9;
            z = 'a';

        }
    }
    class program1
    {
 void test()
        {
            // writing these values to console
            Console.Write("Value of x = " + x + "\nValue of X = " + X + "\n" + "\nValue of z = 
            " + z + "\n");
        }    }
}

Output
local variable

Below shows how and where we can access local variables.

using System;

namespace variable
{
    class Program
    {
        static void Main(string[] args)
        {
            // declaration
            int x;
            double X;
            char z;

            // initialization 
            x = 10;
            X = 13.9;
           z = 'a';

            // writing these values to console
            Console.Write("Value of x = " + x + "\nValue of X = " + X + "\n"+ "Value of z = 
            " + z+"\n");
        }
    }
}

Output
local variable access

Global

Global variables are the ones that are accessible everywhere in the program. i.e. they can be accessed outside the boundary in which they are created. C# does not support Global Variables directly. However, we can create a static class which contains global variables.
Its basic syntax is

    static class class-Name
    {
        public static data-type global-variable-name;
    }

when a static class is created its members are part of a type, not a single instance i.e. we don’t need to create an instance of the static class to access its members. We can access them by class name instead.
Below example will show us how to use a global variable in C#.
Example Program

using System;

namespace global_variable
{
    static class global_variable_example
    {
        public static int x;
    }
    class Program
    {
        static void Main(string[] args)
        {
            // initializing global variable x in another class.
            global_variable_example.x = 10;

            // writing its value to console.
            Console.Write("Value of Global Variable x = "+ global_variable_example.x + "\n");
        }
    }
}

Output
global variable

Leave a Reply

Your email address will not be published. Required fields are marked *