Home »
C#.Net
var keyword in C#
C# var keyword: Here, we are going to learn about the var keyword in C#, what is var keyword, how to use it n C#?
Submitted by IncludeHelp, on March 07, 2019
C# var keyword
In C#, var is a keyword, it is used to declare an implicit type variable, which specifies the type of a variable based on initialized value.
Syntax:
var variable_name = value;
C# code to demonstrate example of 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