- 自动属性
public string Name { get; set; }
- 对象与集合的初始化
//自动推断类型//集合的初始化 var Products=new List<Product>{ new Product{Name="苹果",Price=4.5M,CategoryNumber=100111}, new Product{Name="橘子",Price=2.5M,CategoryNumber=101010}, new Product{Name="柚子",Price=4.5M,CategoryNumber=131312}, new Product{Name="西红柿",Price=3.0M,CategoryNumber=100121}, new Product{Name="茄子",Price=1.5M,CategoryNumber=133121} }; //自动推断类型//对象的初始化 var product = new Product { Name = "苹果", Price = 3.7m, CategoryNumber = 101 };
- 扩展方法
1、对接口运用扩展方法
2、创建过滤扩展方法 - 使用lambda表达式
k => k.Price
- 使用自动类型接口
class Program { static void Main(string[] args) { //自动推断类型 var product = new Product { Name = "苹果", Price = 3.7m, CategoryNumber = 101 }; Console.WriteLine(product.CategoryNumber); } public class Product { public string Name { get; set; } public decimal Price { get; set; } public int CategoryNumber { get; set; } } }
- 使用匿名类型
var Category = new { CategoryNumber = 10010, CategoryName = "食品" };
- 执行语言集成查询(LINQ)
var Products=new List<Product>{ new Product{Name="苹果",Price=4.5M,CategoryNumber=100111}, new Product{Name="橘子",Price=2.5M,CategoryNumber=101010}, new Product{Name="柚子",Price=4.5M,CategoryNumber=131312}, new Product{Name="西红柿",Price=3.0M,CategoryNumber=100121}, new Product{Name="茄子",Price=1.5M,CategoryNumber=133121} }; //linq 查询1(查询语法) var pro1 = from t in Products orderby t.Price descending select new { t.Name, t.Price }; //linq 查询2(点语法/链式语法) var pro2 = Products.OrderByDescending(k => k.Price).Take(3).Select(k => new { k.Name, k.Price });
- 使用Async方法
运用async和await关键字 - 使用委托
//委托方法 Func<Product, bool> fun = delegate(Product n) { return n.Price > 3; }; var pro3 = Products.OrderByDescending(fun); //或者(委托简写) var pro4 = Products.OrderByDescending(k=>k.Price>3);