zoukankan      html  css  js  c++  java
  • ASP.NET中基本语言特性

    1. 自动属性
      public string Name { get; set; }
    2. 对象与集合的初始化
      //自动推断类型//集合的初始化
                  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
                  };
    3. 扩展方法
      1、对接口运用扩展方法
      2、创建过滤扩展方法
    4. 使用lambda表达式
      k => k.Price
    5. 使用自动类型接口
      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; }
              }
          }
      View Code
    6. 使用匿名类型
      var Category = new
                  {
                      CategoryNumber = 10010,
                      CategoryName = "食品"
                  };
    7. 执行语言集成查询(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 });
      View Code
    8. 使用Async方法
      运用async和await关键字
    9. 使用委托
      //委托方法
                  Func<Product, bool> fun = delegate(Product n)
                  {
                      return n.Price > 3;
                  };
                  var pro3 = Products.OrderByDescending(fun);
                  //或者(委托简写)
                  var pro4 = Products.OrderByDescending(k=>k.Price>3);
  • 相关阅读:
    摄像机模型 (Camera Model)
    TP中如何用IF
    Mysql连接报错:1130-host ... is not allowed to connect to this MySql server如何处理
    LNMP环境源码搭建
    Linux之不得不说的init(Linux启动级别的含义 init 0-6)
    PHP 生成毫秒时间戳
    Linux Bash Shell字符串截取
    Linux crontab任务调度
    下载百度文库文档
    关于java socket中的read方法阻塞问题
  • 原文地址:https://www.cnblogs.com/Qos8/p/5988377.html
Copyright © 2011-2022 走看看