0 var is a keyword which is anonymous type and was introduced in C# 3.0. var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time. A var variable must be initialized at the time of declaration which decide data type.
It is mostly helpful when you write LINQ statements and you need customised results, follow below snippets:
- var empQuery =
- from emp in employees
- select new { prod.Name, prod.Qual};
-
- foreach (var v in empQuery)
- {
- Console.WriteLine("Name={0}, Price={1}", v.Name, v.Qual);
- }