C# Variables

In C#, variable is storage location where a value is stored.

For example:

int x= 1;

in the above example, variable name is 'x' and variable if of "int" type and it's value is '1'

When we declare variable, it has a name and a data type. A data type determines what values can be assigned to the variable, for instance integers, strings, or boolean value.

These variables are then used in different line of codes to get desried output. Variables are usually declared with default values, like in the above example default or we can initial value of 'x' is '1'.

Syntax for declaring variables and initializing it in C#

<data type> <name> = <value>;

Data type = integer, string, boolean or float

name = name can be anything which is relevant in the program

C# Variables types

Type Description Size Range
int whole number 4 –2,147,483,648 to 2,147,483,647
long Whole numbers (bigger range) 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float Floating-point numbers 4 +/–3.4 × 10^38
double Double precision (more accurate) floating-point numbers 8 +/–1.7 × 10^308
decimal Monetary values 16 28 significant figures
char Single character 2 N/A
bool Boolean 1 True or False ( 1 / 0)
string sequence of characters 2 per characters N/A

Let's create a sample program declaring various types of variables in it.

using System;

namespace VariablesInCSharp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //string variables
            string name = "Vikas"; 
            string country= "India";
            //int variables
            int age = 30;
            //float variable
            float salary= 50500.00f;
           
           
            Console.WriteLine(name);
            Console.WriteLine(country);
            Console.WriteLine(age);            
            Console.WriteLine(salary);
           
            //changing country value here
            country= "United States";
            Console.WriteLine(country);
        }
    }
}

In the above example, we have three types of variables declared

       //string variables
            string name = "Vikas"; 
            string country= "India";
            //int variables
            int age = 30;
            //float variable
            float salary= 50500.00f;
           

Output:

Vikas
India
30
50500
United States

Notice, in the above code when printing value of "Country" second time it is changed to "United States", as we assingned it new value

           //changing country value here
            country= "United States";

Var keyword (Implicit type variable)

Variables at a method scope can be implicitly typed using the var keyword. The variables are always strongly types, but with var the type is inferred by C# compiler from the right side of the assignment.

It is a special type of variable, which does not need to define data type. We can declare an implicit type variable by using var keyword, the type of variable is identified at the time of compilation depending upon the value assigned to it.

take a look at the below examples:

var   a = 10;		// a is of type int.
var   str = "Hello World";	// str is of type string
var   f = 3.14f;	//  f is of type float  
var   z;   // invalid as now value is provided

Let's take a look at another example

using System;

namespace ImplicitTypeSample
{
    public class Program
    {
       public static void Main(string[] args)
        {
            var name = "John";
            var age = 22;

            Console.WriteLine("{0} is {1} years old", name, age);

            name = "Martin";
            age = 30;

            Console.WriteLine("{0} is {1} years old", name, age);

            Console.WriteLine(name.GetType());
            Console.WriteLine(age.GetType());
        }
    }
}

In the program we have two implicitly typed variables.

            var name = "John";
            var age = 22;

On the left side of the assignment we use the var keyword. The name variable is of string type and the age of int. The types are inferred from the right side value of the assignment, we can check type using .GetType() method as shown below:

Console.WriteLine(name.GetType());
Console.WriteLine(age.GetType());

Output:

John is 22 years old
Martin is 30 years old
System.String
System.Int32


Share Tweet