Although there is nothing wrong with giving a local variable an explicit type, such as int or string, I more than often use the var keyword to implicitly type a local variable. When you do this the compiler provides the type at compile time. It does this by inferring the type of the variable from the expression on the right side of the initialization statement.
You can also use the var keyword in for, foreach and using statements.
Benefits
I feel that using var keyword in my code makes it quicker to write and easier to read. It is certainly my preference when the type is obvious.
Restrictions
- You can not implicitly type class members (i.e. Fields).
- You can only use var when declaring and initializing variable in the same statement
- You can not initialize with a null value
- You can not initialize with a method group
- You can not initialize with an anonymous function
- You can not initialize multiple variables in the same statement
Local variables examples
int example
var j = 17;
string example
var firstName = "Fred";
List example
var employees = new List<string> { "Fred Blogs", "James Kane", "Jon Jones"};
anonymous type example
var employee = new { Name = "Fred Bloggs", Age = 23 };
IEnumerable example
This example is for the type IEnumerable
var customers = new CustomerQuery().FindAll();
var customersNamedFred = from c in customers
where c.FirstName == "Fred"
select c;
This example is for the type IEnumerable
var customers = new List<Customer>
{
new Customer { FirstName = "Fred", LastName = "Bloggs" },
new Customer { FirstName = "Jacon", LastName = "Creaker" }
};
var customersNamedFred = from c in customers
where c.FirstName == "Fred"
select c;
This example is for the type IEnumerable<‘a> (anonymous type)
var customerTuple = new List<Tuple<string, string>>
{
new Tuple<string, string>("Fred", "Bloggs"),
new Tuple<string, string>("Jacon", "Creaker")
};
var customers = customerTuple.Select(t => new {
FirstName = t.Item1,
LastName = t.Item2 }).ToList();
var customersNamedFred = from c in customers
where c.FirstName == "Fred"
select c;
for and foreach initialization statement examples
for initialization statement example
for (var i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
foreach initialization statement example
var customers = new List<string> { "Fred Blogs", "James Kane", "Jon Jones"};
foreach (var customer in customers)
{
Console.WriteLine(customer);
}
using statement example
using (var httpClient = new HttpClient())
{
var response = httpClient.GetStringAsync("http://www.google.com").Result;
}
Git Hub Repository
I have created a repository on GitHub that included the code examples of Implicit Types. The examples were written as NUnit tests to showcase that the expected type is created.
https://github.com/JonathanWelch/Implicit-Types
Useful links
Implicitly Typed Local Variables (C# Programming Guide)