Variable in C#

Variable Introduction

Variables are essential in every programming language. Variables are used to store the data in the program. So to make it clearer, whenever we need to store the value in the program we need to declare a variable. Now why we need to store the value?


Suppose we are working on a calculator program that accept values from the user and as per options selected by the user like add, sub etc. The program will return the output. In order to execute this program when the user selects math operation, the program should remember the user choice furthermore, when the user provides input values our program should hold the value to perform math operation chosen earlier by the user.

To execute the calculator program, we need to define variables. But to define variables we have to specify its type. I.e. Which specific what type of variable it is. For example, if our program wants the user to enter the number then the variable which holds the data should be Int type. If we want to accept text values from the user, then the data type should string.
Defining variables


Below is some example of variable declaration, so “no” is variable which holds the value 6. Whenever we use “no” variable in our program it will represent its value as 6.

int no = 6;
string Name = ”Alice”;
float price = 100.29;

Note: C#, is type-safe language which mean if variable type is int then we can only assign integer values to the variable,

Example

int no = “6”;// it will give compile type error

Addition program source code

using System;

namespace Applications
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            int y = 6;
            int result = x + y;
            Console.WriteLine("The sum is: " + result);
            Console.ReadLine();
        }
    }
}

In below example we have made use of one variable whose name is “result” and the type of value it can store is “int”.

There are two parts to a variable in most computer languages: the definition and the initialization.
Defining a variable means, telling the computer what type of value it can hold, and what is the name of that variable. In our example, we can simply define a variable like:

int x;

Initializing a variable means providing value to the variable. In our example, we can provide value to the variable in the following manner:

x = 5

we can also combine the definition and initialization of the variable into one line like this:

int x = 5;

Explicit Type Variable

When we define the variable with its type its called Explicit Type Variable. In an explicit type variable it makes code more readable leaves no room for confusion.

For example

float radius = 4.5;

In above example we have variable called radius and its float type

Implicit Type Variable

Implicit Type Variable is a new feature provide by .Net, it does not force user to specify its type instead it will automatically evaluate variable type based on its expression used to initialize the variable.

For example:

var radius = 4.5;

Base of right sight expression compiler will evaluate “radius” variable type is float.

Note: there is no difference while declaring variable in implicit or explicit way.

Variable Scope

Variable can be declared inside of a method or inside of a class. Variable scope means the region of the program from where the variable is accessible. In C#, an easy way to understand scope the variable in curly braces ({}). In which curly brace variable defines the scope is limited to that curly brace

Local variable:

Variables that are declared inside a method, function are called local variables. Local variables usage is limited only in statements that are inside that function or block of code. Local variables can not be accessible outside of the method or function:

For example: in below program variable “x” , ”y” , “result” are local variable and can be accessible only in main method only.

using System;

namespace Applications
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            int y = 6;
            int result = x + y;
            Console.WriteLine("The sum is: " + result);
            Console.ReadLine();
        }
    }
}

Member variable

Member variables are defined inside a class, usually on top of the class. The member variables will hold their value throughout the lifetime of your program.

A member variable can be accessed by any function. That is, a member variable is available for use throughout your entire program after its declaration.

For example in below program variable “x” , ”y” , “result” are member variable and can be accessible in any method of a class.

using System;

namespace Applications
{
    class Program
    {
        static int x = 15;
        static int y = 6;
        static int result;
        static void Main(string[] args)
        {
            Console.WriteLine($"The number are {x}, {y}");
            Console.ReadLine();
        }

        static void Add()
        {
            Console.WriteLine($"Add is: {x + y}");
        }

        static void Sub()
        {
            Console.WriteLine($"Sub is: {x - y}");
        }
    }
}

Note: Class is used to group all data member and data methods in to one unit, you can read more about the class.


Leave a Reply

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