tnblog
首页
视频
资源
登录
什么时候才能领悟,取之越多失之越多
排名
5
文章
229
粉丝
15
评论
7
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

C# 11 的新特性和改进前瞻,.NET新特性

5809人阅读 2023/6/7 22:10 总访问:1162988 评论:0 收藏:0 手机
分类: .NET

前言

.NET 7 的开发还剩下一个多月就要进入 RC,C# 11 的新特性和改进也即将敲定。在这个时间点上,不少新特性都已经实现完毕并合并入主分支C# 11 包含的新特性和改进非常多,类型系统相比之前也有了很大的增强,在确保静态类型安全的同时大幅提升了语言表达力。那么本文就按照方向从 5 个大类来进行介绍,一
起来提前看看 C# 11 的新特性和改进都有什么。

1. 类型系统的改进

抽象和虚静态方法

C# 11 开始将 abstract 和 virtual 引入到静态方法中,允许开发者在接口中编写抽象和虚静态方法。接
口与抽象类不同,接口用来抽象行为,通过不同类型实现接口来实现多态;而抽象类则拥有自己的状态,通过各子类型继承父类型来实现多态。这是两种不同的范式。在 C# 11 中,虚静态方法的概念被引入,在接
口中可以编写抽象和虚静态方法了。

  1. interface IFoo
  2. {
  3. // 抽象静态方法
  4. abstract static int Foo1();
  5. // 虚静态方法
  6. virtual static int Foo2()
  7. {
  8. return 42;
  9. }
  10. }
  11. struct Bar : IFoo
  12. {
  13. // 隐式实现接口方法
  14. public static int Foo1()
  15. {
  16. return 7;
  17. }
  18. }
  19. Bar.Foo1(); // ok

由于运算符也属于静态方法,因此从 C# 11 开始,也可以用接口来对运算符进行抽象了。

  1. interface ICanAdd where T : ICanAdd
  2. {
  3. abstract static T operator +(T left, T right);
  4. }

这样我们就可以给自己的类型实现该接口了,例如实现一个二维的点 Point:

  1. record struct Point(int X, int Y) : ICanAdd
  2. {
  3. // 隐式实现接口方法
  4. public static Point operator +(Point left, Point right)
  5. {
  6. return new Point(left.X + right.X, left.Y + right.Y);
  7. }
  8. }

然后我们就可以对两个 Point 进行相加了:

  1. var p1 = new Point(1, 2);
  2. var p2 = new Point(2, 3);
  3. Console.WriteLine(p1 + p2); // Point { X = 3, Y = 5 }

除了隐式实现接口之外,我们也可以显式实现接口:

  1. record struct Point(int X, int Y) : ICanAdd
  2. {
  3. // 显式实现接口方法
  4. static Point ICanAdd.operator +(Point left, Point right)
  5. {
  6. return new Point(left.X + right.X, left.Y + right.Y);
  7. }
  8. }

不过用显示实现接口的方式的话,+ 运算符没有通过 public 公开暴露到类型 Point 上,因此我们需要通 过接口来调用 + 运算符,这可以利用泛型约束来做到:

  1. var p1 = new Point(1, 2);
  2. var p2 = new Point(2, 3);
  3. Console.WriteLine(Add(p1, p2)); // Point { X = 3, Y = 5 }
  4. T Add(T left, T right) where T : ICanAdd
  5. {
  6. return left + right;
  7. }

对于不是运算符的情况,则可以利用泛型参数来调用接口上的抽象和静态方法:

  1. void CallFoo1() where T : IFoo
  2. {
  3. T.Foo1();
  4. }
  5. Bar.Foo1(); // error
  6. CallFoo(); // ok
  7. struct Bar : IFoo
  8. {
  9. // 显式实现接口方法
  10. static void IFoo.Foo1()
  11. {
  12. return 7;
  13. }
  14. }

此外,接口可以基于另一个接口扩展,因此对于抽象和虚静态方法而言,我们可以利用这个特性在接口上实现多态。

  1. CallFoo(); // 5 5
  2. CallFoo(); // 6 4
  3. CallFoo(); // 3 7
  4. CallFooFromIA(); // 1
  5. CallFooFromIB(); // 2
  6. void CallFoo() where T : IC
  7. {
  8. CallFooFromIA();
  9. CallFooFromIB();
  10. }
  11. void CallFooFromIA() where T : IA
  12. {
  13. Console.WriteLine(T.Foo());
  14. }
  15. void CallFooFromIB() where T : IB
  16. {
  17. Console.WriteLine(T.Foo());
  18. }
  19. interface IA
  20. {
  21. virtual static int Foo()
  22. {
  23. return 1;
  24. }
  25. }
  26. interface IB
  27. {
  28. virtual static int Foo()
  29. {
  30. return 2;
  31. }
  32. }
  33. interface IC : IA, IB
  34. {
  35. static int IA.Foo()
  36. {
  37. return 3;
  38. }
  39. static int IB.Foo()
  40. {
  41. return 4;
  42. }
  43. }
  44. struct Bar1 : IC
  45. {
  46. public static int Foo()
  47. {
  48. return 5;
  49. }
  50. }
  51. struct Bar2 : IC
  52. {
  53. static int IA.Foo()
  54. {
  55. return 6;
  56. }
  57. }
  58. struct Bar3 : IC
  59. {
  60. static int IB.Foo()
  61. {
  62. return 7;
  63. }
  64. }
  65. struct Bar4 : IA, IB { }

同时,.NET 7 也利用抽象和虚静态方法,对基础库中的数值类型进行了改进。在 System.Numerics 中新增了大量的用于数学的泛型接口,允许用户利用泛型编写通用的数学计算代码:

  1. using System.Numerics;
  2. V Eval(T a, U b, V c)
  3. where T : IAdditionOperators
  4. where U : IMultiplyOperators
  5. {
  6. return (a + b) * c;
  7. }
  8. Console.WriteLine(Eval(3, 4, 5)); // 35
  9. Console.WriteLine(Eval(3.5f, 4.5f, 5.5f)); // 44

泛型 attribute

C# 11 正式允许用户编写和使用泛型 attribute,因此我们可以不再需要使用 Type 来在 attribute 中存 储类型信息,这不仅支持了类型推导,还允许用户通过泛型约束在编译时就能对类型进行限制。

  1. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  2. class FooAttribute : Attribute where T : INumber
  3. {
  4. public T Value { get; }
  5. public FooAttribute(T v)
  6. {
  7. Value = v;
  8. }
  9. }
  10. [Foo(3)] // ok
  11. [Foo(4.5f)] // ok
  12. [Foo("test")] // error
  13. void MyFancyMethod() { }

ref 字段和 scoped ref

C# 11 开始,开发者可以在 ref struct 中编写 ref 字段,这允许我们将其他对象的引用存储在一个 ref struct 中:

  1. int x = 1;
  2. Foo foo = new(ref x);
  3. foo.X = 2;
  4. Console.WriteLine(x); // 2
  5. ref struct Foo
  6. {
  7. public ref int X;
  8. public Foo(ref int x)
  9. {
  10. X = ref x;
  11. }
  12. }

可以看到,上面的代码中将 x 的引用保存在了 Foo 中,因此对 foo.X 的修改会反映到 x 上。如果用户没有对 Foo.X 进行初始化,则默认是空引用,可以利用 Unsafe.IsNullRef 来判断一个 ref 是否为空:

  1. ref struct Foo
  2. {
  3. public ref int X;
  4. public bool IsNull => Unsafe.IsNullRef(ref X);
  5. public Foo(ref int x)
  6. {
  7. X = ref x;
  8. }
  9. }

这里可以发现一个问题,那就是 ref field 的存在,可能会使得一个 ref 指向的对象的生命周期被扩展而导致错误,例如:

  1. Foo MyFancyMethod()
  2. {
  3. int x = 1;
  4. Foo foo = new(ref x);
  5. return foo; // error
  6. }
  7. ref struct Foo
  8. {
  9. public Foo(ref int x) { }
  10. }

上述代码编译时会报错,因为 foo 引用了局部变量 x,而局部变量 x 在函数返回后生命周期就结束了,但是返回 foo 的操作使得 foo 的生命周期比 x 的生命周期更长,这会导致无效引用的问题,因此编译器检 测到了这一点,不允许代码通过编译。但是上述代码中,虽然 foo 确实引用了 x,但是 foo 对象本身并没有长期持有 x 的引用,因为在构造函数返回后就不再持有对 x 的引用了,因此这里按理来说不应该报错。于是 C# 11 引入了 scoped 的概念,允许开发者显式标注 ref 的生命周期,标注了 scoped 的 ref 表示 这个引用的生命周期不会超过当前函数的生命周期:

  1. Foo MyFancyMethod()
  2. {
  3. int x = 1;
  4. Foo foo = new(ref x);
  5. return foo; // ok
  6. }
  7. ref struct Foo
  8. {
  9. public Foo(scoped ref int x) { }
  10. }

这样一来,编译器就知道 Foo 的构造函数不会使得 Foo 在构造函数返回后仍然持有 x 的引用,因此上述 代码就能安全通过编译了。如果我们试图让一个 scoped ref 逃逸出当前函数的话,编译器就会报错:

  1. ref struct Foo
  2. {
  3. public ref int X;
  4. public Foo(scoped ref int x)
  5. {
  6. X = ref x; // error
  7. }
  8. }

如此一来,就实现了引用安全。利用 ref 字段,我们可以很方便地实现各种零开销设施,例如提供一个多 种方法访问颜色数据的 ColorView:

  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. var color = new Color { R = 1, G = 2, B = 3, A = 4 };
  5. color.RawOfU32[0] = 114514;
  6. color.RawOfU16[1] = 19198;
  7. color.RawOfU8[2] = 10;
  8. Console.WriteLine(color.A); // 74
  9. [StructLayout(LayoutKind.Explicit)]
  10. struct Color
  11. {
  12. [FieldOffset(0)] public byte R;
  13. [FieldOffset(1)] public byte G;
  14. [FieldOffset(2)] public byte B;
  15. [FieldOffset(3)] public byte A;
  16. [FieldOffset(0)] public uint Rgba;
  17. public ColorView RawOfU8 => new(ref this);
  18. public ColorView RawOfU16 => new(ref this);
  19. public ColorView RawOfU32 => new(ref this);
  20. }
  21. ref struct ColorView where T : unmanaged
  22. {
  23. private ref Color color;
  24. public ColorView(ref Color color)
  25. {
  26. this.color = ref color;
  27. }
  28. [DoesNotReturn] private static ref T Throw() => throw new IndexOutOfRangeException();
  29. public ref T this[uint index]
  30. {
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. get
  33. {
  34. unsafe
  35. {
  36. return ref (sizeof(T) * index >= sizeof(Color) ?
  37. ref Throw() :
  38. ref Unsafe.Add(ref Unsafe.AsRef(Unsafe.AsPointer(ref color)), (int)index));
  39. }
  40. }
  41. }
  42. }

在字段中,ref 还可以配合 readonly 一起使用,用来表示不可修改的 ref,例如:- ref int:一个 int 的引用

  • readonly ref int:一个 int 的只读引用
  • ref readonly int:一个只读 int 的引用
  • readonly ref readonly int:一个只读 int 的只读引用
    这将允许我们确保引用的安全,使得引用到只读内容的引用不会被意外更改。当然,C# 11 中的 ref 字段 和 scoped 支持只是其完全形态的一部分,更多的相关内容仍在设计和讨论,并在后续版本中推出。

文件局部类型

C# 11 引入了新的文件局部类型可访问性符号 file,利用该可访问性符号,允许我们编写只能在当前文件 中使用的类型:

  1. // A.cs
  2. file class Foo
  3. {
  4. // ...
  5. }
  6. file struct Bar
  7. {
  8. // ...
  9. }

如此一来,如果我们在与 Foo 和 Bar 的不同文件中使用这两个类型的话,编译器就会报错:

  1. // A.cs
  2. var foo = new Foo(); // ok
  3. var bar = new Bar(); // ok
  4. // B.cs
  5. var foo = new Foo(); // error
  6. var bar = new Bar(); // error

这个特性将可访问性的粒度精确到了文件,对于代码生成器等一些要放在同一个项目中,但是又不想被其他人接触到的代码而言将会特别有用。

required 成员

C# 11 新增了 required 成员,标记有 required 的成员将会被要求使用时必须要进行初始化,例如:

  1. var foo = new Foo(); // error
  2. var foo = new Foo { X = 1 }; // ok
  3. struct Foo
  4. {
  5. public required int X;
  6. }

开发者还可以利用 SetsRequiredMembers 这个 attribute 来对方法进行标注,表示这个方法会初始化 required 成员,因此用户在使用时可以不需要再进行初始化:

  1. using System.Diagnostics.CodeAnalysis;
  2. var p = new Point(); // error
  3. var p = new Point { X = 1, Y = 2 }; // ok
  4. var p = new Point(1, 2); // ok
  5. struct Point
  6. {
  7. public required int X;
  8. public required int Y;
  9. [SetsRequiredMembers]
  10. public Point(int x, int y)
  11. {
  12. X = x;
  13. Y = y;
  14. }
  15. }

利用 required 成员,我们可以要求其他开发者在使用我们编写的类型时必须初始化一些成员,使其能够正确地使用我们编写的类型,而不会忘记初始化一些成员。

2. 运算改进

checked 运算符

C# 自古以来就有 checked 和 unchecked 概念,分别表示检查和不检查算术溢出:

  1. byte x = 100;
  2. byte y = 200;
  3. unchecked
  4. {
  5. byte z = (byte)(x + y); // ok
  6. }
  7. checked
  8. {
  9. byte z = (byte)(x + y); // error
  10. }

在 C# 11 中,引入了 checked 运算符概念,允许用户分别实现用于 checked 和 unchecked 的运算符:

  1. struct Foo
  2. {
  3. public static Foo operator +(Foo left, Foo right) { ... }
  4. public static Foo operator checked +(Foo left, Foo right) { ... }
  5. }
  6. var foo1 = new Foo(...);
  7. var foo2 = new Foo(...);
  8. var foo3 = unchecked(foo1 + foo2); // 调用 operator +
  9. var foo4 = checked(foo1 + foo2); // 调用 operator checked +

对于自定义运算符而言,实现 checked 的版本是可选的,如果没有实现 checked 的版本,则都会调用 unchecked 的版本。

无符号右移运算符

C# 11 新增了 >>> 表示无符号的右移运算符。此前 C# 的右移运算符 >> 默认是有符号的右移,即:右移 操作保留符号位,因此对于 int 而言,将会有如下结果:

  1. -1 >> 1 = -1
  2. -1 >> 2 = -1
  3. -1 >> 3 = -1
  4. -1 >> 4 = -1
  5. // ...

而新的 >>> 则是无符号右移运算符,使用后将会有如下结果:

  1. -1 >>> 1 = 2147483647
  2. -1 >>> 2 = 1073741823
  3. -1 >>> 3 = 536870911
  4. -1 >>> 4 = 268435455
  5. // ...

这省去了我们需要无符号右移时,需要先将数值转换为无符号数值后进行计算,再转换回来的麻烦,也能避免不少因此导致的意外错误。

移位运算符放开类型限制

C# 11 开始,移位运算符的右操作数不再要求必须是 int,类型限制和其他运算符一样被放开了,因此结合上面提到的抽象和虚静态方法,允许我们声明泛型的移位运算符了:

  1. interface ICanShift where T : ICanShift
  2. {
  3. abstract static T operator <<(T left, T right);
  4. abstract static T operator >>(T left, T right);
  5. }

当然,上述的场景是该限制被放开的主要目的。然而,相信不少读者读到这里心中都可能会萌生一个邪恶的想法,没错,就是 cin 和 cout!虽然这种做法在 C# 中是不推荐的,但该限制被放开后,开发者确实能编写类似的代码了:

  1. using static OutStream;
  2. using static InStream;
  3. int x = 0;
  4. _ = cin >> To(ref x); // 有 _ = 是因为 C# 不允许运算式不经过赋值而单独成为一条语句
  5. _ = cout << "hello" << " " << "world!";
  6. public class OutStream
  7. {
  8. public static OutStream cout = new();
  9. public static OutStream operator <<(OutStream left, string right)
  10. {
  11. Console.WriteLine(right);
  12. return left;
  13. }
  14. }
  15. public class InStream
  16. {
  17. public ref struct Ref
  18. {
  19. public ref T Value;
  20. public Ref(ref T v) => Value = ref v;
  21. }
  22. public static Ref To(ref T v) => new (ref v);
  23. public static InStream cin = new();
  24. public static InStream operator >>(InStream left, Ref right)
  25. {
  26. var str = Console.Read(...);
  27. right.Value = int.Parse(str);
  28. }
  29. }

IntPtr、UIntPtr 支持数值运算

C# 11 中,IntPtr 和 UIntPtr 都支持数值运算了,这极大的方便了我们对指针进行操作:

  1. UIntPtr addr = 0x80000048;
  2. IntPtr offset = 0x00000016;
  3. UIntPtr newAddr = addr + (UIntPtr)offset; // 0x8000005E

当然,如同 Int32 和 int、Int64 和 long 的关系一样,C# 中同样存在 IntPtr 和 UIntPtr 的等价简写 ,分别为 nint 和 nuint,n 表示 native,用来表示这个数值的位数和当前运行环境的内存地址位数相同 :

  1. nuint addr = 0x80000048;
  2. nint offset = 0x00000016;
  3. nuint newAddr = addr + (nuint)offset; // 0x8000005E

3. 模式匹配改进

列表模式匹配

C# 11 中新增了列表模式,允许我们对列表进行匹配。在列表模式中,我们可以利用 [ ] 来包括我们的模 式,用 _ 代指一个元素,用 .. 代表 0 个或多个元素。在 .. 后可以声明一个变量,用来创建匹配的子列表,其中包含 .. 所匹配的元素。例如:

  1. var array = new int[] { 1, 2, 3, 4, 5 };
  2. if (array is [1, 2, 3, 4, 5]) Console.WriteLine(1); // 1
  3. if (array is [1, 2, 3, ..]) Console.WriteLine(2); // 2
  4. if (array is [1, _, 3, _, 5]) Console.WriteLine(3); // 3
  5. if (array is [.., _, 5]) Console.WriteLine(4); // 4
  6. if (array is [1, 2, 3, .. var remaining])
  7. {
  8. Console.WriteLine(remaining[0]); // 4
  9. Console.WriteLine(remaining.Length); // 2
  10. }

当然,和其他的模式一样,列表模式同样是支持递归的,因此我们可以将列表模式与其他模式组合起来使用:

  1. var array = new string[] { "hello", ",", "world", "~" };
  2. if (array is ["hello", _, { Length: 5 }, { Length: 1 } elem, ..])
  3. {
  4. Console.WriteLine(elem); // ~
  5. }

除了在 if 中使用模式匹配以外,在 switch 中也同样能使用:

  1. var array = new string[] { "hello", ",", "world", "!" };
  2. switch (array)
  3. {
  4. case ["hello", _, { Length: 5 }, { Length: 1 } elem, ..]:
  5. // ...
  6. break;
  7. default:
  8. // ...
  9. break;
  10. }
  11. var value = array switch
  12. {
  13. ["hello", _, { Length: 5 }, { Length: 1 } elem, ..] => 1,
  14. _ => 2
  15. };
  16. Console.WriteLine(value); // 1

对 Span 的模式匹配

在 C# 中,Span 和 ReadOnlySpan 都可以看作是字符串的切片,因此 C# 11 也为这两个类型添加了字符串模式匹配的支持。例如:

  1. int Foo(ReadOnlySpan span)
  2. {
  3. if (span is "abcdefg") return 1;
  4. return 2;
  5. }
  6. Foo("abcdefg".AsSpan()); // 1
  7. Foo("test".AsSpan()); // 2

如此一来,使用 Span 或者 ReadOnlySpan 的场景也能够非常方便地进行字符串匹配了,而不需要利用 SequenceEquals 或者编写循环进行处理。

4. 字符串处理改进

原始字符串

C# 中自初便有 @ 用来表示不需要转义的字符串,但是用户还是需要将 “ 写成 “” 才能在字符串中包含引 号。C# 11 引入了原始字符串特性,允许用户利用原始字符串在代码中插入大量的无需转移的文本,方便开发者在代码中以字符串的方式塞入代码文本等。原始字符串需要被至少三个 “ 包裹,例如 “”” 和 “”””” 等等,前后的引号数量要相等。另外,原始字符串的缩进由后面引号的位置来确定,例如:

  1. var str = """
  2. hello
  3. world
  4. """;

此时 str 是:

  1. hello
  2. world

而如果是下面这样:

  1. var str = """
  2. hello
  3. world
  4. """;

str 则会成为:

  1. hello
  2. world

这个特性非常有用,例如我们可以非常方便地在代码中插入 JSON 代码了:

  1. var json = """
  2. {
  3. "a": 1,
  4. "b": {
  5. "c": "hello",
  6. "d": "world"
  7. },
  8. "c": [1, 2, 3, 4, 5]
  9. }
  10. """;
  11. Console.WriteLine(json);
  12. /*
  13. {
  14. "a": 1,
  15. "b": {
  16. "c": "hello",
  17. "d": "world"
  18. },
  19. "c": [1, 2, 3, 4, 5]
  20. }
  21. */

UTF-8 字符串

C# 11 引入了 UTF-8 字符串,我们可以用 u8 后缀来创建一个 ReadOnlySpan,其中包含一个 UTF-8 字符串:

  1. var str1 = "hello world"u8; // ReadOnlySpan
  2. var str2 = "hello world"u8.ToArray(); // byte[]

UTF-8 对于 Web 场景而言非常有用,因为在 HTTP 协议中,默认编码就是 UTF-8,而 .NET 则默认是 UTF-16 编码,因此在处理 HTTP 协议时,如果没有 UTF-8 字符串,则会导致大量的 UTF-8 和 UTF-16 字符串 的相互转换,从而影响性能。有了 UTF-8 字符串后,我们就能非常方便的创建 UTF-8 字面量来使用了,不再需要手动分配一个 byte[] 然后在里面一个一个硬编码我们需要的字符。

字符串插值允许换行

C# 11 开始,字符串的插值部分允许换行,因此如下代码变得可能:

  1. var str = $"hello, the leader is {group
  2. .GetLeader()
  3. .GetName()}.";

这样一来,当插值的部分代码很长时,我们就能方便的对代码进行格式化,而不需要将所有代码挤在一行。

5. 其他改进

struct 自动初始化

C# 11 开始,struct 不再强制构造函数必须要初始化所有的字段,对于没有初始化的字段,编译器会自动 做零初始化:

  1. struct Point
  2. {
  3. public int X;
  4. public int Y;
  5. public Point(int x)
  6. {
  7. X = x;
  8. // Y 自动初始化为 0
  9. }
  10. }

支持对其他参数名进行 nameof

C# 11 允许了开发者在参数中对其他参数名进行 nameof,例如在使用 CallerArgumentExpression 这一 attribute 时,此前我们需要直接硬编码相应参数名的字符串,而现在只需要使用 nameof 即可:

  1. void Assert(bool condition, [CallerArgumentExpression(nameof(condition))] string expression = "")
  2. {
  3. // ...
  4. }

这将允许我们在进行代码重构时,修改参数名 condition 时自动修改 nameof 里面的内容,方便的同时减 少出错。

自动缓存静态方法的委托

C# 11 开始,从静态方法创建的委托将会被自动缓存,例如:

  1. void Foo()
  2. {
  3. Call(Console.WriteLine);
  4. }
  5. void Call(Action action)
  6. {
  7. action();
  8. }

此前,每执行一次 Foo,就会从 Console.WriteLine 这一静态方法创建一个新的委托,因此如果大量执行 Foo,则会导致大量的委托被重复创建,导致大量的内存被分配,效率极其低下。在 C# 11 开始,将会自动缓存静态方法的委托,因此无论 Foo 被执行多少次,Console.WriteLine 的委托只会被创建一次,节省了 内存的同时大幅提升了性能。

总结

从 C# 8 开始,C# 团队就在不断完善语言的类型系统,在确保静态类型安全的同时大幅提升语言表达力, 从而让类型系统成为编写程序的得力助手,而不是碍手碍脚的限制。本次更新还完善了数值运算相关的内容,使得开发者利用 C# 编写数值计算方法时更加得心应手。另外,模式匹配的探索旅程也终于接近尾声,引入列表模式之后,剩下的就只有字典模式和活动模式了,模式匹配是一个非常强大的工具,允许我们像对字符串使用正则表达式那样非常方便地对数据进行匹配。总的来说 C# 11 的新特性和改进内容非常多,每一 项内容都对 C# 的使用体验有着不小的提升。在未来的 C# 中还计划着角色和扩展等更加令人激动的新特性,让我们拭目以待。

原文:https://zhuanlan.zhihu.com/p/539727051


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

vs2022及以上版本net core net6 net8 添加dll引用

现在不是右键添加dll引用了。 右键添加com引用: 然后在下面点击浏览现在你需要引用的dll即可: 其实添加项目引用里边也...

JAVA8 十大新特性详解

前言: Java 8 已经发布很久了,很多报道表明Java 8 是一次重大的版本升级。在Java Code Geeks上已经有很多介绍Java 8新特...

c 8.0新特性 合并赋值 ?? =

用法很简单: noteWalls ??= new List&lt;NoteWall&gt;(); 合并赋值,8.0里边的语法糖。左边为空时执行,相当于为空就...

.net9新特性,.net9介绍。ASP.net core 9.0 新增功能

LINQ 针对各种常见情况进行了优化。例如,当底层数组、集合或可枚举对象为空时,Take 和 DefaultIfEmpty 等方法的返回速度...

net core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

cAPS.net 保存base64位格式的图片

publicvoidUpload() { //取出图片对应的base64位字符 stringimgBase=Request[&quot;imgBase&quot;]; //c#里边的base6...

Quartz.net实例动态改变周期调度。misfire、Cron

Quartz:Java编写的开源的任务调度作业框架 类似Timer之类定时执行的功能,但是更强大Quartz.NET:是把Quartz转成C# NuGet...

.net Windows服务发布、安装、卸载、监听脚本。服务调试

一、脚本 为方便不用每次都去写安装卸载的脚本1.安装脚本@echooff @echo开始安装【服务】 %SystemRoot%\Microsoft.NET\Fr...

c、VB.net中全角半角转换方法

///&lt;summary&gt; ///转全角的函数(SBCcase) ///&lt;/summary&gt; ///&lt;paramname=&quot;input&quot;&gt;任意字符串...

.net mvc分部页,.net core分部页

.net分部页的三种方式第一种:@Html.Partial(&quot;_分部页&quot;)第二种:@{ Html.RenderPartial(&quot;分部页&quot;);}...

C.net 配合小程序实现经过第三方服务器中转文件

某些时候,微信小程序前段上传文件的时候需要经过第三方服务器再将文件上传到客户的服务器;操作如下:1:(小程序内向中端服...

.net实现QQ邮箱发送邮件功能

1、微软已经帮我们封装好了发送邮件的类MailMessage,MailMessage类构造一些邮件信息,然后通过SmtpClient进行邮件发送。Mai...

StackExchange.Redis操作redis(net core支持)

官方git开源地址https://github.com/StackExchange/StackExchange.Redis官方文档在docs里边都是官方的文档通过nuget命令下...

windows 自带的netsh进行端口映射

使用netsh 把本地任意ip的25566端口 映射到192.168.81.234的25565端口netshinterfaceportproxyaddv4tov4listenaddress=0.0....

确保.net程序始终以管理员身份运行

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; ...
我重伤倒地,但还活着。