首页 技术 正文
技术 2022年11月15日
0 收藏 667 点赞 3,788 浏览 1853 个字

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开头二进制串
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,093
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,569
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,417
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,190
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908