var keyword in C#

Last Updated : 5 Sep, 2025

In C#, the var keyword is used for implicit type declaration.

  • When you declare a variable with var, the compiler infers the type based on the value assigned to it at compile time.
  • The type is fixed after compilation, it’s not dynamic.
  • The variable must be initialized at the time of declaration.

Syntax:

var variable_name = value;

Example 1:

csharp
using System;
using System.Text;

class GFG {

    static void Main(string[] args)
    {
        var a = 637;
        var b = -9721087085262;

        // to print their type of variables
        Console.WriteLine("value of a {0}, type {1}", a, a.GetType());
        Console.WriteLine("value of b {0}, type {1}", b, b.GetType());
    }
}

Output
value of a 637, type System.Int32
value of b -9721087085262, type System.Int64

Example 2:

csharp
using System;
using System.Text;

namespace Test {

class GFG {

    static void Main(string[] args)
    {
        var c = 120.23f;
        var d = 150.23m;
        var e = 'G';
        var f = "Geeks";

        // to print their type of variables
        Console.WriteLine("value of c {0}, type {1}", c, c.GetType());
        Console.WriteLine("value of d {0}, type {1}", d, d.GetType());
        Console.WriteLine("value of e {0}, type {1}", e, e.GetType());
        Console.WriteLine("value of f {0}, type {1}", f, f.GetType());

        Console.ReadLine();
    }
}
}

Output
value of c 120.23, type System.Single
value of d 150.23, type System.Decimal
value of e G, type System.Char
value of f Geeks, type System.String
Comment
Article Tags:

Explore