C#3.0特性之列表对象的赋值更容易

时间:2022-12-12 22:57:46
    List<Customer> customers = new List<Customer>
{
new Customer { Id = 1, Name="Dave", City="Sarasota" },
new Customer { Id = 2, Name="John", City="Tampa" },
new Customer { Id = 3, Name="Abe", City="Miami" }
};
而在2.0时代,我们为列表对象赋值要这样写:

List<Customer> customers2 = new List<Customer>();
customers2.Add(new Customer { Id = 1, Name = "Dave", City = "Sarasota" });
customers2.Add(new Customer { Id = 2, Name = "John", City = "Tampa" });
customers2.Add(new Customer { Id = 3, Name = "Abe", City = "Miami" });
而在对象单个实体赋值时也就方便了,而且可读性也强了

2.0时代:

Customer customer = new Customer();

customer.Id = 1;
customer.Name = "zzl";
customer.City = "beijing";

3.0时代
Customer customer2 = new Customer
{
Id = 1,
Name = "zzl",
City = "beijing"
};

作者:仓储大叔,张占岭,
荣誉:微软MVP