1

C#11新特性整理 - Eric zhou

 1 year ago
source link: https://www.cnblogs.com/tianqing/p/17062055.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C#11新特性整理

假期中有时间,整理了C#11的各个新特性,简单分享给大家。

一、使用VSCode新建一个.NET7.0的Console工程

23525-20230119183214251-1016579769.png

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <RootNamespace>_NET7</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

二、泛型属性

C # 11为开发人员提供了一种编写自定义通用属性的方法。

public class CommonAttribute<T> : Attribute
{
    public T Property {get;set;}

    public CommonAttribute() 
    {
        
    }

    public CommonAttribute(T realvalue) :this()
    {
        Property = realvalue;
    }
}

新建一个类,在属性上使用这个通用属性注解。原先是多个属性注解类型,现在一个泛型属性就可以搞定了

public class User
{
    [CommonAttribute<int>]
    public int UserID { get; set;}

    [CommonAttribute<string>("defaultValue")]
    public string UserName { get; set;}
}

三、非空校验

原先在C#10中,可以这么写实现参数非空校验

public User GetUser(string name)
{
    ArgumentNullException.ThrowIfNull(nameof(name));
    // Rest of code
}

在C#11中,原计划在参数上加2个!就实现了非空校验,实际测试是不支持的。

error CS8989: 不支持 'parameter null-checking' 功能。

23525-20230119192605216-956430330.png

 四、字符串内的换行符

字符串内插的 { 和 } 字符内的文本现在可以跨多个行。 

{ 和 } 标记之间的文本分析为 C#。 允许任何合法 C#(包括换行符)。

使用此功能可以更轻松地读取使用较长 C# 表达式的字符串内插,例如模式匹配 switch 表达式或 LINQ 查询。

五、列表模式匹配

列表模式扩展了模式匹配,以匹配列表或数组中的元素序列。

例如,当 sequence 为数组或三个整数(1、2 和 3)的列表时,sequence is [1, 2, 3] 为 true

可以使用任何模式(包括常量、类型、属性和关系模式)来匹配元素。

弃元模式 (_) 匹配任何单个元素,新的范围模式 (..) 匹配零个或多个元素的任何序列。

写几个列子看看:

var l1 = new[] { 1, 2, 3, 4, 5 };
if(l1 is [1, 2, 3, 4, 5] && l1 is [_,_,_,_,5])
{
    Console.WriteLine("List Pattern Matched");
}
23525-20230119194159731-120401825.png

 上述代码中,[_,_,_,_,5] ,可以这么理解:匹配长度为5且最后一个元素为5的任何集合。

23525-20230119194342177-1980796334.png

 同时,切片模式进一步放大了列表模式的威力。例如,如果需要将任何少于或等于5个元素的集合与最后一个元素5匹配,则可以将上述模式修改为

if (l1 is [.., 5])
{
    Console.WriteLine("List Pattern Matched 3");
}

   这段代码中 [..,5] 等同于 l1.Length>=1 && l1[^1]==5.

  调试输出:List Pattern Matched 3

   再写一个Switch模式匹配的例子

var l1 = new[] { 1, 2, 3, 4,5 };
var l2 = new[] { 1, 3, 5 };
var l3 = new[] { 1};
var l4 = new[] { 9 };
var l5 = new[] { 1,6 };

string PatternTest(int[] collection) => collection switch
{
    [1, 2, 3, 4, 5] => "first", // list
    [1, 3, .., 5] => "second", // slice in list
    [1, _] => "third", // list
    [1, ..] => "fourth", //slice in list
    [..] => "fifth" // slice
};

Console.WriteLine(PatternTest(l1));
Console.WriteLine(PatternTest(l2));
Console.WriteLine(PatternTest(l3));
Console.WriteLine(PatternTest(l4));
Console.WriteLine(PatternTest(l5));
23525-20230119194849049-1710940558.png

实际输出:

first
second
fourth
fifth
third

 六、原始字符串

原始字符串是字符串的一种新格式。

原始字符串可以包含任意文本,包括空格、新行、嵌入引号和其他特殊字符,无需转义字符(这个很关键、也很简单)

原始字符串以至少三个双引号 (""") 字符开头。 它以相同数量的双引号字符结尾。

通常,原始字符串在单个行上使用三个双引号来开始字符串,在另一行上用三个双引号来结束字符串。 左引号之后、右引号之前的换行符不包括在最终内容中。

详细的示例,可以看前段时间整理的一篇文章。

C#11新特性-Raw string literals原始字符串研究、示例

以上是这几天研究的C#11的新特性。分享给大家。

2023/1/19


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK