Local Type Inference (aka Implicitly Typed Local variables)
var keyword
Emphasis on local, as in local scope.
NOTE: var cannot be used in the following cases
As a return value from function
Uninitialized variables
Null assignment
using System;
using System.Collections.Generic;
using System.Linq;
public class csharp3_2
{
public static void RunSnippet()
{
var num = 1;
Console.WriteLine("Typeof num is {0}", num.GetType());
var name = "rajesh";
Console.WriteLine("Typeof name is {0}", num.GetType());
string[] countries = new string[] { "India", "US", "Israel", "China"};
var iCountries = from c in countries
where c.StartsWith("I")
select c;
foreach(var c in iCountries)
Console.WriteLine(" Country starting with I {0}", c);
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format
("---\nThe following error occurred while executing
the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}