Wednesday, December 5, 2018
Home > C# > Variables in C#

C# Variables

We can define c# variables as a name given to memory location to store a value in it. A variable can be accessed anywhere in the program depending upon its scope. And a computer program can change these values at any time during its execution depending on the code. Thus, a variable has a name, it also has a data type which indicates the type of data we can store in it. Access to variables in c# is restricted by the scope of those variables.

Variable Declaration

When we give a name to a memory space it is called variable declaration. The basic syntax for declaring c# variables is:
data_type variable_name;

data_type is any of the available data types in c#. It includes user-defined data types as well. It indicates what type or kind of data we can save in the variable.

variable_name is the name we use to save a value in the memory.
Rules for naming the variables in c# are almost same as for naming them 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

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

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

Variable initialization

When we store a value for the first time in a memory location it is called variable initialization.
There are two ways to do this:
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++, variables 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

The 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 variables access example

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 global variables 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 variables in c# example

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.