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.
In this Repository you can find code examples of Implicit Types in use. The examples were written as NUnit tests to showcase that the expected type is created.
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.
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
Listed below are the examples that can be found in the Visual Studio Solution.
var j = 17;
var firstName = "Fred";
var employees = new List<string> { "Fred Blogs", "James Kane", "Jon Jones"};
var employee = new { Name = "Fred Bloggs", Age = 23 };
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 (var i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
var customers = new List<string> { "Fred Blogs", "James Kane", "Jon Jones"};
foreach (var customer in customers)
{
Console.WriteLine(customer);
}
using (var httpClient = new HttpClient())
{
var response = httpClient.GetStringAsync("http://www.google.com").Result;
}