var keyword in C#

C# var keyword: Here, we will find out about the var keyword in C#, what is var keyword, how to utilize it n C#?

C# var keyword:

In C#, var is a keyword, it is utilized to proclaim a certain sort variable, which determines the kind of a variable dependent on introduced esteem.

Syntax:

    var variable_name = value;

C# code to exhibit an example of a var keyword:

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = 10;
            var b = 10.23;
            var c = 10.23f;
            var d = 10.23m;
            var e = 'X';
            var f = "Hello";

            Console.WriteLine("value of a {0}, type {1}", a, a.GetType());
            Console.WriteLine("value of b {0}, type {1}", b, b.GetType());
            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());

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

value of a 10, type System.Int32
value of b 10.23, type System.Double
value of c 10.23, type System.Single
value of d 10.23, type System.Decimal
value of e X, type System.Char
value of f Hello, type System.String

Leave a Comment