C# 3.0 新功能
C# 3.0 新功能
隐式类型的本地变量和数组
对象初始化设定项
集合初始化设定项
自动实现的属性
匿名类型
一:隐式类型的本地变量和数组
交错数组:每个元素都是由数组组成.
var a = 5;
var s = "Hello";
//匿名类型
var anon = new {Name="Ben",Age=26 };
var s = "Hello";
//匿名类型
var anon = new {Name="Ben",Age=26 };
代码
string[] words = {"aPPLE","BIUeBeRry","cHerry" };
var upperLowerWords = from w in words
select
new { Upper = w.ToUpper(), Lower = w.ToLower() };//匿名类型
foreach (var ul in upperLowerWords)
{
Response.Write(String.Format("Uppercase:{0},Lowercase:{1}",ul.Upper,ul.Lower)+"<br/>");
}
var upperLowerWords = from w in words
select
new { Upper = w.ToUpper(), Lower = w.ToLower() };//匿名类型
foreach (var ul in upperLowerWords)
{
Response.Write(String.Format("Uppercase:{0},Lowercase:{1}",ul.Upper,ul.Lower)+"<br/>");
}
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> people = new List<Person>
{
new Person{FirstName="Scott",LastName="LEE",Age=32},
new Person{FirstName="Susanne",LastName="LEE",Age=32},
new Person{FirstName="Bill",LastName="Gates",Age=50}
};
var results = people.Where(p=>p.LastName=="LEE");
foreach (var item in results)
{
Response.Write(item.FirstName+","+item.LastName+","+item.Age+"<br/>");
}
}
}
public class Person
{
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> people = new List<Person>
{
new Person{FirstName="Scott",LastName="LEE",Age=32},
new Person{FirstName="Susanne",LastName="LEE",Age=32},
new Person{FirstName="Bill",LastName="Gates",Age=50}
};
var results = people.Where(p=>p.LastName=="LEE");
foreach (var item in results)
{
Response.Write(item.FirstName+","+item.LastName+","+item.Age+"<br/>");
}
}
}
public class Person
{
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
}