zoukankan      html  css  js  c++  java
  • C# -.Net新特性

    C# 5.0

    VS2012 引入,参见:https://www.cnblogs.com/ctcx/p/5177635.html

    调用者信息特性

    CallerMemberNameAttribute | CallerFilePathAttribute | CallerLineNumberAttribute
    

    .NET Framework 4.5 中新增,用于请求编译器在编译过程中进行代码的转换 。

    使用方式:直接调用即可

    public static void TraceMessage(string message, string errCode, 
                    [CallerMemberNameAttribute] string memberName = "",
                    [CallerFilePathAttribute] string filePath = "",
                    [CallerLineNumberAttribute] int lineNumber = 0)
    

    若要在 .NET Framework 4.0 中使用,需自定义特性

    namespace System.Runtime.CompilerServices
    {
        [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
        public class CallerMemberNameAttribute : Attribute
        { }
    
        [AttributeUsage(AttributeTargets.Parameter, Inherited = false )]
        public class CallerFilePathAttribute : Attribute
        { }
    
        [AttributeUsage(AttributeTargets.Parameter, Inherited = false )]
        public class CallerLineNumberAttribute : Attribute
        { }
    }
    

    关键字async和await

    简化异步编程,建议首先了解C# 4.0引入的:Task

    在Lambda表达式中用循环变量

    C#5.0中纠正循环变量覆盖,无须在循环中引入临时变量,直接常规编码即可。

    C# 6.0

    VS2015 引入,参考:https://www.cnblogs.com/dotnet261010/p/9147707.html

    using static

    命名空间语法糖,导入静态类

    字符串嵌入值 | 空值运算符

    $"{表达式|属性字段值}"  //简化string.Format表达式
    
    // null值亦可调用,程序不会报错,也不会输出任何值
    string name = null;  name?.ToString(); 
    

    对象初始化器 | 异常过滤器

    IDictionary<int, string> dictNew = new Dictionary<int, string>() {   
       [4] = "first",    [5] = "second" //索引方式初始化
    };
    
    try {} //满足条件才进入catch
    catch (Exception e) when (匹配条件) { }
    

    同时支持在catch和finally中使用await运算符。

    nameof表达式

    用于变量、函数、类或命名空间,返回其名称,可应用于反射等场景。

    属性/方法使用Lambda表达式

    public double Distance => Math.Sqrt((X * X) + (Y * Y));
    public void Print() => Console.WriteLine(Name);
    

    该功能在C#7.0中已有进一步增强。

     

    C# 7.0

    VS2017 引入,参考:https://www.cnblogs.com/cncc/p/7698543.html

    模式匹配

    [1]. is表达式

    [2]. case分支引入类型匹配和条件判断

    元组Tuples:强烈推荐

    • ValueTuple支持语义上的字段命名 
    • ValueTuple是值类型(Struct)

    元组解构:Deconstruct 方法成员(实例或扩展)

    // 实例签名
    public void Deconstruct(out type variable1, out type variable2...)
    // 扩展签名
    public static void Deconstruct(this type instance, out type variable1, out type variable2...)
    

    局部函数

    本质是 internal 修饰的静态函数

    其他重要特性

    • out变量:无需预先声明,内联声明即可
    • ref引用强化:允许获取某个变量(引用类型)的局部引用
    • 数字分割:可以按照一定的位数用“_”进行分割
    • 二进制文本:0b开头二进制串
  • 相关阅读:
    AutoIT练习
    AutoIT键盘鼠标模拟组件
    javascript class 定义
    JIRA
    vs 命令窗口 常用命令
    jquery 获取参数 plugin
    THE VALUE OF TIME
    哈佛图书馆的二十条训言
    优秀javascript js组件集锦
    UVA 11205 The broken pedometer
  • 原文地址:https://www.cnblogs.com/wjcx-sqh/p/10685533.html
Copyright © 2011-2022 走看看