隐式类型的局部变量 下面一个简单的例子演示了var声明局部变量的各种方式span stylebackground-color:#ffffe6span stylecolor:#000000 span stylecolor:#008000////span span stylecolor:#008000///Summary通过var声明的变量都是由编译器来推断其类型/Summary/span span stylecolor:#008000////span span stylecolor:#008000//i被编译时int类型/span var i 5; span stylecolor:#008000//s被编译为string类型/span var s span stylecolor:#a31515strongSunny/strong/span; span stylecolor:#008000//a被编译为int[]类型/span var a span stylecolor:#0000ffnew/span[] { 0, 1, 2 }; span stylecolor:#008000//expr被编译为IEnumerableCustomer类型/span var expr from c span stylecolor:#0000ffin/span customers span stylecolor:#0000ffwhere/span c.City span stylecolor:#a31515strongLondon/strong/span select c; span stylecolor:#008000//anon被编译为匿名类类型/span var anon span stylecolor:#0000ffnew/span { Name span stylecolor:#a31515strongSunny/strong/span, Age 22 }; span stylecolor:#008000//list被编译为Listint类型/span var list span stylecolor:#0000ffnew/span Listspan stylecolor:#0000ffint/span();/span/span注意var关键字并不意味着“变体”也不表示该变量是松散类型的变量或后期绑定的变量。它指示表示由编译器确定和适当分配的类型。var关键字可在下面的上下文中使用如上所示的局部变量在方法中声明的局部变量在for初始化语句中span stylebackground-color:#ffffe6span stylecolor:#000000span stylecolor:#0000fffor/span(var x 1; x 10; x) /span/span在foreach初始化语句中span stylebackground-color:#ffffe6span stylecolor:#000000span stylecolor:#0000ffforeach/span(var item span stylecolor:#0000ffin/span list){...}/span/span在using语句中span stylebackground-color:#ffffe6span stylecolor:#000000span stylecolor:#0000ffusing/span (var file span stylecolor:#0000ffnew/span StreamReader(span stylecolor:#a31515strongC:\\myfile.txt/strong/span)) {...}/span/spanvar和匿名类型在使用匿名类型初始化变量时如果需要在以后访问对象的属性则必须将该变量声明为 var。这在 LINQ 查询表达式中很常见。匿名对象的类型名由编译器生成并且不能在源代码中使用它的属性类型由编译器来推断。span stylebackground-color:#ffffe6span stylecolor:#000000 span stylecolor:#0000ffstatic/span span stylecolor:#0000ffvoid/span Main() { span stylecolor:#0000ffstring/span[] words { span stylecolor:#a31515strongaPPLE/strong/span, span stylecolor:#a31515strongBlUeBeRrY/strong/span, span stylecolor:#a31515strongcHeRry/strong/span }; span stylecolor:#008000// If a query produces a sequence of anonymous types, /span span stylecolor:#008000// then use var in the foreach statement to access the properties./span var upperLowerWords from w span stylecolor:#0000ffin/span words select span stylecolor:#0000ffnew/span { Upper w.ToUpper(), Lower w.ToLower() }; span stylecolor:#008000// Execute the query/span span stylecolor:#0000ffforeach/span (var ul span stylecolor:#0000ffin/span upperLowerWords) { Console.WriteLine(span stylecolor:#a31515strongUppercase: {0}, Lowercase: {1}/strong/span, ul.Upper, ul.Lower); } } /span/span输出span stylebackground-color:#ffffe6span stylecolor:#000000Uppercase: APPLE, Lowercase: apple Uppercase: BLUEBERRY, Lowercase: blueberry Uppercase: CHERRY, Lowercase: cherry /span/span备注下列限制适用于隐式类型的变量声明只有在同一语句中声明和初始化局部变量时才能使用 var不能将该变量初始化为 null、方法组或匿名函数。不能将 var 用于类范围的域。由 var 声明的变量不能用在初始化表达式中。换句话说表达式 int i (i 20) 是合法的但表达式 var i (i 20) 则会产生编译时错误。不能在同一语句中初始化多个隐式类型的变量。如果范围中有一个名为 var 的类型则 var 关键字将解析为该类型名称而不作为隐式类型局部变量声明的一部分进行处理。2. Linq和泛型类型LINQ 查询基于泛型类型IEnumerableT 是一个接口通过该接口可以使用 foreach 语句来枚举泛型集合类。泛型集合类都支持 IEnumerableT。Linq查询中的IEnumerableT变量LINQ 查询变量类型化为 IEnumerableT 或派生类型如 IQueryableT。当您看到类型化为 IEnumerableCustomer 的查询变量时这只意味着在执行该查询时该查询将生成包含零个或多个 Customer 对象的序列。span stylebackground-color:#ffffe6span stylecolor:#000000 span stylecolor:#0000ffclass/span Program { span stylecolor:#0000ffstatic/span span stylecolor:#0000ffvoid/span Main(span stylecolor:#0000ffstring/span[] args) { ListCustomer customers span stylecolor:#0000ffnew/span ListCustomer() { span stylecolor:#0000ffnew/span Customer(span stylecolor:#a31515strongJack/strong/span, span stylecolor:#a31515strongChen/strong/span, span stylecolor:#a31515strongLondon/strong/span), span stylecolor:#0000ffnew/span Customer(span stylecolor:#a31515strongSunny/strong/span,span stylecolor:#a31515strongPeng/strong/span, span stylecolor:#a31515strongShenzhen/strong/span), span stylecolor:#0000ffnew/span Customer(span stylecolor:#a31515strongTom/strong/span,span stylecolor:#a31515strongCat/strong/span,span stylecolor:#a31515strongLondon/strong/span) }; span stylecolor:#008000//使用IEnumerableT作为变量/span IEnumerableCustomer customerQuery from cust span stylecolor:#0000ffin/span customers span stylecolor:#0000ffwhere/span cust.City span stylecolor:#a31515strongLondon/strong/span select cust; span stylecolor:#0000ffforeach/span (Customer customer span stylecolor:#0000ffin/span customerQuery) { Console.WriteLine(customer.LastName span stylecolor:#a31515strong,/strong/span customer.FirstName); } Console.Read(); } } span stylecolor:#0000ffpublic/span span stylecolor:#0000ffclass/span Customer { span stylecolor:#0000ffpublic/span Customer(span stylecolor:#0000ffstring/span firstName, span stylecolor:#0000ffstring/span lastName, span stylecolor:#0000ffstring/span city) { FirstName firstName; LastName lastName; City city; } span stylecolor:#0000ffpublic/span span stylecolor:#0000ffstring/span FirstName { get; set; } span stylecolor:#0000ffpublic/span span stylecolor:#0000ffstring/span LastName { get; set; } span stylecolor:#0000ffpublic/span span stylecolor:#0000ffstring/span City { get; set; } }/span/span使用var局部变量让编译器处理泛型类型声明在上面声明查询变量时使用了IEnumerableCustomer作为变量类型如果想偷懒请使用var来声明变量让编译器去处理变量类型。span stylebackground-color:#ffffe6span stylecolor:#000000var customerQuery2 from cust span stylecolor:#0000ffin/span customers span stylecolor:#0000ffwhere/span cust.City span stylecolor:#a31515strongLondon/strong/span select cust; span stylecolor:#0000ffforeach/span(var customer span stylecolor:#0000ffin/span customerQuery2) { Console.WriteLine(customer.LastName span stylecolor:#a31515strong, /strong/span customer.FirstName); } /span/span3. Linq的基本查询操作Linq的基本查询操作包括以下几个获取数据源筛选排序分组联接选择span stylebackground-color:#ffffe6span stylecolor:#000000 span stylecolor:#0000ffclass/span Program { span stylecolor:#0000ffpublic/span span stylecolor:#0000ffstatic/span ListCustomer GetCustomers() { ListCustomer customers span stylecolor:#0000ffnew/span ListCustomer { span stylecolor:#0000ffnew/span Customer{Namespan stylecolor:#a31515strongSunny Peng/strong/span, Cityspan stylecolor:#a31515strongShenZhen/strong/span}, span stylecolor:#0000ffnew/span Customer{Namespan stylecolor:#a31515strongDevon/strong/span, Cityspan stylecolor:#a31515strongLondon/strong/span}, span stylecolor:#0000ffnew/span Customer{Namespan stylecolor:#a31515strongJayZ/strong/span, Cityspan stylecolor:#a31515strongParis/strong/span}, span stylecolor:#0000ffnew/span Customer{Namespan stylecolor:#a31515strongJohn Smith/strong/span, Cityspan stylecolor:#a31515strongLondon/strong/span}, span stylecolor:#0000ffnew/span Customer{Namespan stylecolor:#a31515strongJerry Chou/strong/span, Cityspan stylecolor:#a31515strongParis/strong/span}, }; span stylecolor:#0000ffreturn/span customers; } span stylecolor:#0000ffstatic/span span stylecolor:#0000ffvoid/span Main(span stylecolor:#0000ffstring/span[] args) { } } span stylecolor:#0000ffpublic/span span stylecolor:#0000ffclass/span Customer { span stylecolor:#0000ffpublic/span span stylecolor:#0000ffstring/span Name { get; set; } span stylecolor:#0000ffpublic/span span stylecolor:#0000ffstring/span City { get; set; } }/span/span说明下面的几个和Customer相关的例子中使用的数据源都是如上所示。