盘点C# 8.0中好用的特性

news/2024/5/19 22:08:41 标签: c#, C#8.0语法糖, .netcore

增加引用类型可为null:

//如果参数t是null,则会发出警告
public static void Test<T?>(T t){            
}

模式匹配

1、swith 多条件匹配

## 1 元组模式 
int a = 1;
int b = 10;
 string c = (a, b) switch {
 (1, 10) =>  "123",
  _ =>  "default",
};

### 使用弃元模式
_ = (a, b) switch {
    (1, 10) =>  "123",
     _ =>  "default",
};

## 2 位置模式
    public class PointA
    {
        public int X { get; }
        
        public int Y { get; }
        public int Z { get; }
        public PointA(int x, int y) => (X, Y) = (x, y);
        public PointA(int x, int y,int z) => (X, Y,Z) = (x, y,z);
        public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
        public void Deconstruct(out int x, out int y,out int z) => (x, y,z) = (X, Y,Z);
    }

static string GetLocation(PointA point) => point switch
        {
            (0, 0) => "1",
            (0, 0, 0) => "1",
            var (x, y) when x > 0 && y > 0 =>"2",
            var (_, _) => "3",// 当 x 或 y 不为null时
            _ => "4"
   };

Using 声明

## 先熟悉下使用 此处 C#7就能用了

using static System.Math;
using Tasks = System.Threading.Tasks;

using (FileStream fileStream = new FileStream("", FileMode.Open))  {  }

##  C#8
using FileStream fileStream2 = new FileStream("", FileMode.Open);

接口特性增加

— 可以声明一个属性
— 方法可以实现(不再是只定义)

  public interface MyInterface
    {
        public int Age { get; set; }
        void Method1(int age)
        {
            Console.WriteLine(age);
        }
    }

Readonly 成员

public readonly struct Coords
  {
      public Coords(double x, double y)
        {
            X = x;
            Y = y;
        }

      public double X { get; set; }
      public double Y { get; set; }

      public override string ToString() => $"({X}, {Y})";

  }

---- 结构体 局部只读

public struct Coords{

      public double X { get; set; }
      public double Y { get; set; }

      private int counter;
      public  int Counter
        {
            readonly get => counter;
            set => counter = value;
        }

     /*public Coords(double x, double y, int _counter)
        {
            X = x;
            Y = y;
            counter = _counter;
        }*/

      public override string ToString() => $"({X}, {Y})";

      public readonly double Sum()
        {
            /*X = 200;
			Y = 200;*/
            return X + Y;
        }


  }

静态本地函数

总结

支持禁止从封闭范围捕获状态的本地函数。

详细设计

声明的本地函数 static 无法从封闭范围中捕获状态。 因此, this 局部函数中不提供封闭范围内的局部变量、参数和 static 。
static局部函数不能通过隐式或显式或引用来引用实例成员 this base 。
static局部函数可以引用 static 封闭范围内的成员。

static int z = 0;

 int InnerMethod(){
    int y = 5;
    int x = 7;
       
    return LocalStaticFunction(x, y);
    //静态本地函数
    static int LocalStaticFunction(int left, int right) {
                return z + left + right;
    }
}

异步流

C#8.0之前 支持迭代器方法和异步方法,但不支持同时作为迭代器和异步的方法。 我们应该通过允许 await 使用新的迭代器形式来纠正这种情况 async ,它将返回 IAsyncEnumerable 或 IAsyncEnumerator 而不是 IEnumerable 或 IEnumerator , IAsyncEnumerable 在新的中使用 await foreach 。 IAsyncDisposable接口还用于启用异步清理。

新增接口: IAsyncDisposable 、IAsyncEnumerable/IAsyncEnumerator

通常情况下,任何时候清理资源所需的时间可能会很有用,例如关闭文件 (需要刷新) ,取消注册回调并提供一种方法来了解注销完成的时间等。

namespace System
{
    public interface IAsyncDisposable
    {
        ValueTask DisposeAsync();
    }
}

与一样 Dispose , DisposeAsync 多次调用都是可接受的,并且第一次调用之后的调用应被视为 nops(意味着后续调用都应该是 nop),返回同步完成的任务 (DisposeAsync 不需要线程安全的,并且无需支持) 的并发调用。
名称解释:nop不执行任何操作

namespace System.Collections.Generic
{
    public interface IAsyncEnumerable<out T>
    {
        IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
    }

    public interface IAsyncEnumerator<out T> : IAsyncDisposable
    {
        ValueTask<bool> MoveNextAsync();
        T Current { get; }
    }
}
IAsyncEnumerator<T> enumerator = enumerable.GetAsyncEnumerator();
try
{
    while (await enumerator.MoveNextAsync())
    {
        Use(enumerator.Current);
    }
}
finally { await enumerator.DisposeAsync(); }
static async void Main(string[] args) {
    IAsyncEnumerable<string> asyncEnumerable = AsyncEnumerableTest();
    IAsyncEnumerator<string> asyncEnumerator = asyncEnumerable.GetAsyncEnumerator();
    try{
       while (await asyncEnumerator.MoveNextAsync()) {
           Console.WriteLine(asyncEnumerator.Current);
   		} 
    }
    finally { await asyncEnumerator.DisposeAsync(); }
}

## 异步迭代器,但await不能在这些迭代器的主体中使用
private static async IAsyncEnumerable<string> AsyncEnumerableTest()
{
    await Task.Delay(100);
    yield return "001";
    yield return "002";
    yield return "003";
    yield return "004";
    yield return "005";
}
  • Task MoveNextAsync(); T current { get; }:使用 Task 将支持使用缓存的任务对象来表示同步、成功的 MoveNextAsync 调用,但异步完成仍需要分配。 通过返回 ValueTask ,我们允许枚举器对象自身实现 IValueTaskSource 并用作从返回的的后备 ValueTask MoveNextAsync ,这反过来允许大大降低开销。
  • ValueTask<(bool, T)> MoveNextAsync();:更难使用,但这意味着它 T 不再是协变的。

foreach 引入异步

若要强制 foreach 改为仅考虑异步 api,请按 await 如下所示插入:

await foreach (var i in enumerable)
var enumerable = ...; 
await foreach (T item in enumerable) {    ... } 

### 转换为的等效项:

var enumerable = ...;
var enumerator = enumerable.GetAsyncEnumerator();
try
{
    while (await enumerator.MoveNextAsync())
    {
       T item = enumerator.Current;
       ...
    }
}
finally
{
    await enumerator.DisposeAsync(); // omitted, along with the try/finally, if the enumerator doesn't expose DisposeAsync
}

ConfigureAwait
此基于模式的编译允许 ConfigureAwait 通过扩展方法在所有等待中使用

await foreach (T item in enumerable.ConfigureAwait(false))
{
   ...
}

###这将基于我们还将添加到 .NET 的类型,可能会 System.Threading.Tasks.Extensions.dll:

LINQ增加异步方法

List<string> list = new List<string>();
            list.Add("001");
            list.Add("002");
            list.Add("003");
            list.Add("004");
            list.Select(AsyncMethod);

async ValueTask<string> AsyncMethod(string item){
         await Task.Yield();
        return item + "-";
}
List<string> list = new List<string>();
            list.Add("001");
            list.Add("002");
            list.Add("003");
            list.Add("004");
            list.Select(async (item) =>
            {
                await Task.Yield();
                return item + "-";
            });
 public partial class Form1 : Form
    {
        CancellationTokenSource cancellationToken;

        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            cancellationToken = new CancellationTokenSource();
            CancellationToken token = cancellationToken.Token;

            IAsyncEnumerable<int> enumerable = ReadIntAsync(token);

            await Task.Delay(3000);

            //设置要在循环访问时传递到 GetAsyncEnumerator(CancellationToken) 的 CancellationToken。
            //enumerable.WithCancellation(token);
            IAsyncEnumerator<int> enumerator = enumerable.GetAsyncEnumerator();

            try
            {
                while (await enumerator.MoveNextAsync())
                {
                    Debug.WriteLine(enumerator.Current);
                }
            }
            catch (TaskCanceledException ex1)
            {
                Console.WriteLine(ex1.Message);
            }
            catch (OperationCanceledException ex2)
            {
                Console.WriteLine(ex2.Message);
            }
            finally
            {
                await enumerator.DisposeAsync();
            }

        }


        private void button2_Click(object sender, EventArgs e)
        {
			cancellationToken = cancellationToken??new CancellationTokenSource();
            cancellationToken.Cancel();
        }

        
        
        private async IAsyncEnumerable<int> ReadIntAsync([EnumeratorCancellation] CancellationToken _token)
        {
            try
            {
                for (int i = 0; i < 20; i++)
                {
                    //取消任务及其子级
                    if (_token.IsCancellationRequested)
                    {
                        _token.ThrowIfCancellationRequested();
                    }
                    await Task.Delay(200, _token);
                    yield return i;
                }
            }
            finally
            {
                Console.WriteLine("finally");
            }
        }

    }

索引和范围

此功能与提供两个新的运算符,它们允许构造 System.Index 和 System.Range 对象,并使用它们在运行时索引/切片集合。

namespace System
{
    public readonly struct Index
    {
        public Index(int value, bool fromEnd);
    }
}

若要 System.Index 在数组元素访问中使用类型作为参数,需要以下成员:

int System.Index.GetOffset(int length);

int[] arr = new int[6] { 132, 67, 47, 58, 83,100};
Index index = new Index(8, true);
— 偏移量
int offset = index.GetOffset(arr.Length);

的 … 语法 System.Range 需要该 System.Range 类型,以及以下一个或多个成员:

namespace System
{
    public readonly struct Range
    {
        public Range(System.Index start, System.Index end);
        public static Range StartAt(System.Index start);
        public static Range EndAt(System.Index end);
        public static Range All { get; }
    }
}

最后,对于 System.Range 要在数组元素访问表达式中使用的类型值,必须存在以下成员:

//System.Runtime.CompilerServices=> 意味着编译器一开始就可以编译
namespace System.Runtime.CompilerServices
{
    public static class RuntimeHelpers
    {
        public static T[] GetSubArray<T>(T[] array, System.Range range);
    }
}

语言会引入新的范围运算符 x…y 。 它是一个接受两个表达式的二元中缀运算符

System.Range operator ..(Index start = 0, Index end = ^0);

… 将运算符称为 范围运算符

 public Index(int value, bool fromEnd);

运算符 ^ 等价于 Index(int value, bool fromEnd)

示例:

int[] arr = new int[6] { 132, 67, 47, 58, 83,100};
//获取前三个元素 以某某作为截止
int[] vs1 = RuntimeHelpers.GetSubArray(arr, Range.EndAt(3));
//获取后三个元素 以某某作为开始
int[] vs2 = RuntimeHelpers.GetSubArray(arr, Range.StartAt(3));
Console.WriteLine(vs2);
 Range range1 = Range.StartAt(2);//从第三个元素开始 startIndex<=x<lenth
 Range range2 = new Range(2, 4);//从第三个元素开始 到第四个元素 startIndex<=x<endIndex
 var array = new int[] { 1, 2, 3, 4, 5 };
 var slice1 = array[range1];  
 var slice2 = array[range2];  

fromEnd为true 从尾部计算下标 :offset = arr.length-index;

new Index(index, fromEnd: true).GetOffset(array.Length)

var array = new int[] { 1, 2, 3, 4, 5 };
var lastItem = array[^1];    // array[new Index(1, fromEnd: true)]
var slice1 = array[2..^3];    // array[new Range(2, new Index(3, fromEnd: true))]
var slice2 = array[..^3];     // array[Range.EndAt(new Index(3, fromEnd: true))]
var slice3 = array[2..];      // array[Range.StartAt(2)]
var slice4 = array[..];       // array[Range.All]

此外, System.Index 应从进行隐式转换 System.Int32 ,以避免对多维签名重载混合整数和索引的需求。

此新优先级组低于_一元运算符_ ,大于_乘法算术运算符_

内插逐字字符串的增强功能

内插字符串的结构

若要将字符串标识为内插字符串,可在该字符串前面加上 $ 符号。 字符串字面量开头的 $ 和 " 之间不能有任何空格。 若要连接多个内插字符串,请将 $ 特殊字符添加到每个字符串字面量。
具备内插表达式的项的结构如下所示:

 {<interpolationExpression>[,<alignment>][:<formatString>]}
元素描述
interpolationExpression生成需要设置格式的结果的表达式。 null 的字符串表示形式为 String.Empty。
alignment常数表达式,如果值为正,则字符串表示形式为右对齐;如果值为负,则为左对齐。 数值为意味着字符串占位长度,如果小于表达式实际值 则以实际值长度计算。
formatString受表达式结果类型支持的格式字符串。 有关更多信息,请参阅格式字符串组件。
如时间格式:MM/dd/yy H:mm:ss zzz
GUID格式: N 或者 D (见下方示例)
Console.WriteLine($"|{"Left000"}|{"000Right"}|");
Console.WriteLine($"|{"Left",-7}|{"Right",7}|");

---输出结果:--
|Left000|000Right|
|Left   |  Right|
-----

Guid guid = Guid.NewGuid();
Console.WriteLine($"{guid:N}");

小破站: https://www.bilibili.com/video/BV1E24y147Vc
抖茵: https://www.douyin.com/user/self?modal_id=7217801169001844007&showTab=post


http://www.niftyadmin.cn/n/200283.html

相关文章

uniapp - tensorflowjs 之小程序环境集成

一、简介 tensorflowjs&#xff08;简称 tfjs&#xff09;是一个用于使用 JavaScript 进行机器学习开发的库。以下是 tfjs 的官网与 github 仓库&#xff1a; 官网&#xff1a;https://www.tensorflow.org/js?hlzh-cngithub 仓库&#xff1a;https://github.com/tensorflow/…

Windows线程开发

Windows线程开发 Unit01线程 加锁机制&#xff1a;原子锁、互斥锁协调机制&#xff1a;信号量、事件 01线程基础 Windows线程是可以执行的代码的实例。系统是以线程为单位调度程序。一个程序中可以有多个线程&#xff0c;实现多任务的处理Windows线程的特点&#xff1a; 线…

项目的总结

逻辑处理 首先对于版项目来说,工作多是一定的但是又可以看到自己的知识被应用和自己学的不是一无所用。 对于一个项目而言,逻辑是很重要的。理解一个项目的逻辑,我们才能去实施工作画流程图、用例图、原型图、e-r图等等模型 一开始对于一个项目、逻辑是十分混乱的,就需要解耦,把…

令博主崩溃的scanf函数-C语言

目录 前言&#xff1a; 1.了解scanf()、getchar()和putchar 2.scanf输入的奇怪现象 3.scanf格式对回车的处理 4.小谈一下&#xff1a; 5.格式对scanf的重要性 前言&#xff1a; 这篇博客的来源呢&#xff0c;源于一个同学在学习群里发出一段代码&#xff0c;他对代码有疑…

目标检测算法

目标检测算法可以分为两类&#xff1a;基于区域的目标检测算法和基于单阶段的目标检测算法。 基于区域的目标检测算法 基于区域的目标检测算法主要分为两个阶段&#xff1a;生成区域和分类定位。其基本思想是在图像中选择一些可能包含目标的区域&#xff0c;然后对这些区域进行…

人人都是数据分析师-数据分析之数据图表可视化(下)

当前的BI报表、运营同学的汇报报告中数据图表大多为 表格、折线图、柱状图和饼图&#xff0c;但是实际上还有很多具有代表性的可视化图表&#xff0c;因此将对常见的可视化图表进行介绍&#xff0c;希望这些图表可视化方法能够更好的提供数据的可用性。 人人都是数据分析师-数…

第八天并发编程篇

一、简述线程、进程、程序的基本概念&#xff1f; 1.进程&#xff1a; 我们把运行中的程序叫做进程,每个进程都会占用内存与CPU资源,进程与进程之间互相独立. 2.线程&#xff1a; 线程就是进程中的一个执行单元&#xff0c;负责当前进程中程序的执行。一个进程可以包含多个线程…

electron学习-02:electron 热启动功能(解决每次修改代码后都需要重新启动的问题)

安装nodemon npm i -D nodemon配置 启动项 # package.json"scripts": {"start": "nodemon --exec electron . --ext .js,.css,.html"},–ext .js,.css,.html 保存 对应的类型文件后会自动重启&#xff08;如js、css、html文件等&#xff0c;可…