{"id":10005970,"date":"2022-07-22T11:37:57","date_gmt":"2022-07-22T03:37:57","guid":{"rendered":"https:\/\/ityet.com\/?p=10005970"},"modified":"2022-07-22T11:37:57","modified_gmt":"2022-07-22T03:37:57","slug":"c-11-%e7%9a%84%e6%96%b0%e7%89%b9%e6%80%a7%e5%92%8c%e6%94%b9%e8%bf%9b%e5%89%8d%e7%9e%bb","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10005970","title":{"rendered":"C# 11 \u7684\u65b0\u7279\u6027\u548c\u6539\u8fdb\u524d\u77bb"},"content":{"rendered":"<p>.NET 7 \u7684\u5f00\u53d1\u8fd8\u5269\u4e0b\u4e00\u4e2a\u591a\u6708\u5c31\u8981\u8fdb\u5165 RC\uff0cC# 11 \u7684\u65b0\u7279\u6027\u548c\u6539\u8fdb\u4e5f\u5373\u5c06\u6572\u5b9a\u3002\u5728\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\uff0c\u4e0d\u5c11\u65b0\u7279\u6027\u90fd\u5df2\u7ecf\u5b9e\u73b0\u5b8c\u6bd5\u5e76\u5408\u5e76\u5165\u4e3b\u5206\u652f\u3002C# 11 \u5305\u542b\u7684\u65b0\u7279\u6027\u548c\u6539\u8fdb\u975e\u5e38\u591a\uff0c\u7c7b\u578b\u7cfb\u7edf\u76f8\u6bd4\u4e4b\u524d\u4e5f\u6709\u4e86\u5f88\u5927\u7684\u589e\u5f3a\uff0c\u5728\u786e\u4fdd\u9759\u6001\u7c7b\u578b\u5b89\u5168\u7684\u540c\u65f6\u5927\u5e45\u63d0\u5347\u4e86\u8bed\u8a00\u8868\u8fbe\u529b\u3002\u90a3\u4e48\u672c\u6587\u5c31\u6309\u7167\u65b9\u5411\u4ece 5 \u4e2a\u5927\u7c7b\u6765\u8fdb\u884c\u4ecb\u7ecd\uff0c\u4e00\u8d77\u6765\u63d0\u524d\u770b\u770b C# 11 \u7684\u65b0\u7279\u6027\u548c\u6539\u8fdb\u90fd\u6709\u4ec0\u4e48\u3002<\/p>\n<h2>\u7c7b\u578b\u7cfb\u7edf\u7684\u6539\u8fdb<\/h2>\n<h4>\u258c\u62bd\u8c61\u548c\u865a\u9759\u6001\u65b9\u6cd5<\/h4>\n<p>C# 11 \u5f00\u59cb\u5c06\u00a0abstract\u00a0\u548c\u00a0virtual\u00a0\u5f15\u5165\u5230\u9759\u6001\u65b9\u6cd5\u4e2d\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u63a5\u53e3\u4e2d\u7f16\u5199\u62bd\u8c61\u548c\u865a\u9759\u6001\u65b9\u6cd5\u3002<br \/>\u63a5\u53e3\u4e0e\u62bd\u8c61\u7c7b\u4e0d\u540c\uff0c\u63a5\u53e3\u7528\u6765\u62bd\u8c61\u884c\u4e3a\uff0c\u901a\u8fc7\u4e0d\u540c\u7c7b\u578b\u5b9e\u73b0\u63a5\u53e3\u6765\u5b9e\u73b0\u591a\u6001\uff1b\u800c\u62bd\u8c61\u7c7b\u5219\u62e5\u6709\u81ea\u5df1\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u5404\u5b50\u7c7b\u578b\u7ee7\u627f\u7236\u7c7b\u578b\u6765\u5b9e\u73b0\u591a\u6001\u3002\u8fd9\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u8303\u5f0f\u3002<br \/>\u5728 C# 11 \u4e2d\uff0c\u865a\u9759\u6001\u65b9\u6cd5\u7684\u6982\u5ff5\u88ab\u5f15\u5165\uff0c\u5728\u63a5\u53e3\u4e2d\u53ef\u4ee5\u7f16\u5199\u62bd\u8c61\u548c\u865a\u9759\u6001\u65b9\u6cd5\u4e86\u3002<\/p>\n<pre><code>interface IFoo{    \/\/ \u62bd\u8c61\u9759\u6001\u65b9\u6cd5\n    abstract static int Foo1();    \/\/ \u865a\u9759\u6001\u65b9\u6cd5\n    virtual static int Foo2()\n    {        return 42;\n    }\n}struct Bar : IFoo\n{    \/\/ \u9690\u5f0f\u5b9e\u73b0\u63a5\u53e3\u65b9\u6cd5\n    public static int Foo1()\n    {        return 7;\n    }\n}\n\nBar.Foo1(); \/\/ ok<\/code><\/pre>\n<p>\u7531\u4e8e\u8fd0\u7b97\u7b26\u4e5f\u5c5e\u4e8e\u9759\u6001\u65b9\u6cd5\uff0c\u56e0\u6b64\u4ece C# 11 \u5f00\u59cb\uff0c\u4e5f\u53ef\u4ee5\u7528\u63a5\u53e3\u6765\u5bf9\u8fd0\u7b97\u7b26\u8fdb\u884c\u62bd\u8c61\u4e86\u3002<\/p>\n<pre><code>interface ICanAdd&lt;T&gt; where T : ICanAdd&lt;T&gt;\n{    abstract static T operator +(T left, T right);\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7ed9\u81ea\u5df1\u7684\u7c7b\u578b\u5b9e\u73b0\u8be5\u63a5\u53e3\u4e86\uff0c\u4f8b\u5982\u5b9e\u73b0\u4e00\u4e2a\u4e8c\u7ef4\u7684\u70b9\u00a0Point\uff1a<\/p>\n<pre><code>record struct Point(int X, int Y) : ICanAdd&lt;Point&gt;\n{    \/\/ \u9690\u5f0f\u5b9e\u73b0\u63a5\u53e3\u65b9\u6cd5\n    public static Point operator +(Point left, Point right)\n    {        return new Point(left.X + right.X, left.Y + right.Y);\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u4e24\u4e2a\u00a0Point\u00a0\u8fdb\u884c\u76f8\u52a0\u4e86\uff1a<\/p>\n<pre><code>var p1 = new Point(1, 2);var p2 = new Point(2, 3);\nConsole.WriteLine(p1 + p2); \/\/ Point { X = 3, Y = 5 }<\/code><\/pre>\n<p>\u9664\u4e86\u9690\u5f0f\u5b9e\u73b0\u63a5\u53e3\u4e4b\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5b9e\u73b0\u63a5\u53e3\uff1a<\/p>\n<pre><code>record struct Point(int X, int Y) : ICanAdd&lt;Point&gt;\n{    \/\/ \u663e\u5f0f\u5b9e\u73b0\u63a5\u53e3\u65b9\u6cd5\n    static Point ICanAdd&lt;Point&gt;.operator +(Point left, Point right)\n    {        return new Point(left.X + right.X, left.Y + right.Y);\n    }\n}<\/code><\/pre>\n<p>\u4e0d\u8fc7\u7528\u663e\u793a\u5b9e\u73b0\u63a5\u53e3\u7684\u65b9\u5f0f\u7684\u8bdd\uff0c+\u00a0\u8fd0\u7b97\u7b26\u6ca1\u6709\u901a\u8fc7\u00a0public\u00a0\u516c\u5f00\u66b4\u9732\u5230\u7c7b\u578b\u00a0Point\u00a0\u4e0a\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u901a\u8fc7\u63a5\u53e3\u6765\u8c03\u7528\u00a0+\u00a0\u8fd0\u7b97\u7b26\uff0c\u8fd9\u53ef\u4ee5\u5229\u7528\u6cdb\u578b\u7ea6\u675f\u6765\u505a\u5230\uff1a<\/p>\n<pre><code>var p1 = new Point(1, 2);var p2 = new Point(2, 3);\nConsole.WriteLine(Add(p1, p2)); \/\/ Point { X = 3, Y = 5 }T Add&lt;T&gt;(T left, T right) where T : ICanAdd&lt;T&gt;{    return left + right;\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e\u4e0d\u662f\u8fd0\u7b97\u7b26\u7684\u60c5\u51b5\uff0c\u5219\u53ef\u4ee5\u5229\u7528\u6cdb\u578b\u53c2\u6570\u6765\u8c03\u7528\u63a5\u53e3\u4e0a\u7684\u62bd\u8c61\u548c\u9759\u6001\u65b9\u6cd5\uff1a<\/p>\n<pre><code>void CallFoo1&lt;T&gt;() where T : IFoo{\n    T.Foo1();\n}\n\nBar.Foo1(); \/\/ errorCallFoo&lt;Bar&gt;(); \/\/ okstruct Bar : IFoo\n{    \/\/ \u663e\u5f0f\u5b9e\u73b0\u63a5\u53e3\u65b9\u6cd5\n    static void IFoo.Foo1()\n    {        return 7;\n    }\n}<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u63a5\u53e3\u53ef\u4ee5\u57fa\u4e8e\u53e6\u4e00\u4e2a\u63a5\u53e3\u6269\u5c55\uff0c\u56e0\u6b64\u5bf9\u4e8e\u62bd\u8c61\u548c\u865a\u9759\u6001\u65b9\u6cd5\u800c\u8a00\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u7279\u6027\u5728\u63a5\u53e3\u4e0a\u5b9e\u73b0\u591a\u6001\u3002<\/p>\n<pre><code>CallFoo&lt;Bar1&gt;(); \/\/ 5 5CallFoo&lt;Bar2&gt;(); \/\/ 6 4CallFoo&lt;Bar3&gt;(); \/\/ 3 7CallFooFromIA&lt;Bar4&gt;(); \/\/ 1CallFooFromIB&lt;Bar4&gt;(); \/\/ 2void CallFoo&lt;T&gt;() where T : IC{\n    CallFooFromIA&lt;T&gt;();\n    CallFooFromIB&lt;T&gt;();\n}void CallFooFromIA&lt;T&gt;() where T : IA{\n    Console.WriteLine(T.Foo());\n}void CallFooFromIB&lt;T&gt;() where T : IB{\n    Console.WriteLine(T.Foo());\n}interface IA{    virtual static int Foo()\n    {        return 1;\n    }\n}interface IB{    virtual static int Foo()\n    {        return 2;\n    }\n}interface IC : IA, IB{    static int IA.Foo()\n    {        return 3;\n    }    static int IB.Foo()\n    {        return 4;\n    }\n}struct Bar1 : IC\n{    public static int Foo()\n    {        return 5;\n    }\n}struct Bar2 : IC\n{    static int IA.Foo()\n    {        return 6;\n    }\n}struct Bar3 : IC\n{    static int IB.Foo()\n    {        return 7;\n    }\n}struct Bar4 : IA, IB { }<\/code><\/pre>\n<p>\u540c\u65f6\uff0c.NET 7 \u4e5f\u5229\u7528\u62bd\u8c61\u548c\u865a\u9759\u6001\u65b9\u6cd5\uff0c\u5bf9\u57fa\u7840\u5e93\u4e2d\u7684\u6570\u503c\u7c7b\u578b\u8fdb\u884c\u4e86\u6539\u8fdb\u3002\u5728\u00a0System.Numerics\u00a0\u4e2d\u65b0\u589e\u4e86\u5927\u91cf\u7684\u7528\u4e8e\u6570\u5b66\u7684\u6cdb\u578b\u63a5\u53e3\uff0c\u5141\u8bb8\u7528\u6237\u5229\u7528\u6cdb\u578b\u7f16\u5199\u901a\u7528\u7684\u6570\u5b66\u8ba1\u7b97\u4ee3\u7801\uff1a<\/p>\n<pre><code>using System.Numerics;V Eval&lt;T, U, V&gt;(T a, U b, V c) \n    where T : IAdditionOperators&lt;T, U, U&gt;    where U : IMultiplyOperators&lt;U, V, V&gt;{    return (a + b) * c;\n}\n\nConsole.WriteLine(Eval(3, 4, 5)); \/\/ 35Console.WriteLine(Eval(3.5f, 4.5f, 5.5f)); \/\/ 44<\/code><\/pre>\n<h4>\u258c\u6cdb\u578b attribute<\/h4>\n<p>C# 11 \u6b63\u5f0f\u5141\u8bb8\u7528\u6237\u7f16\u5199\u548c\u4f7f\u7528\u6cdb\u578b attribute\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4e0d\u518d\u9700\u8981\u4f7f\u7528\u00a0Type\u00a0\u6765\u5728 attribute \u4e2d\u5b58\u50a8\u7c7b\u578b\u4fe1\u606f\uff0c\u8fd9\u4e0d\u4ec5\u652f\u6301\u4e86\u7c7b\u578b\u63a8\u5bfc\uff0c\u8fd8\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u6cdb\u578b\u7ea6\u675f\u5728\u7f16\u8bd1\u65f6\u5c31\u80fd\u5bf9\u7c7b\u578b\u8fdb\u884c\u9650\u5236\u3002<\/p>\n<pre><code>[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]class FooAttribute&lt;T&gt; : Attribute where T : INumber&lt;T&gt;\n{    public T Value { get; }    public FooAttribute(T v)\n{\n        Value = v;\n    }\n}\n\n[Foo&lt;int&gt;(3)] \/\/ ok[Foo&lt;float&gt;(4.5f)] \/\/ ok[Foo&lt;string&gt;(&quot;test&quot;)] \/\/ errorvoid MyFancyMethod() { }<\/code><\/pre>\n<h4>\u258cref \u5b57\u6bb5\u548c scoped ref<\/h4>\n<p>C# 11 \u5f00\u59cb\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u00a0ref struct\u00a0\u4e2d\u7f16\u5199\u00a0ref\u00a0\u5b57\u6bb5\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u5c06\u5176\u4ed6\u5bf9\u8c61\u7684\u5f15\u7528\u5b58\u50a8\u5728\u4e00\u4e2a\u00a0ref struct\u00a0\u4e2d\uff1a<\/p>\n<pre><code>int x = 1;\nFoo foo = new(ref x);\nfoo.X = 2;\nConsole.WriteLine(x); \/\/ 2ref struct Foo\n{    public ref int X;    \n    public Foo(ref int x)\n    {\n        X = ref x;\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u5c06\u00a0x\u00a0\u7684\u5f15\u7528\u4fdd\u5b58\u5728\u4e86\u00a0Foo\u00a0\u4e2d\uff0c\u56e0\u6b64\u5bf9\u00a0foo.X\u00a0\u7684\u4fee\u6539\u4f1a\u53cd\u6620\u5230\u00a0x\u00a0\u4e0a\u3002<\/p>\n<p>\u5982\u679c\u7528\u6237\u6ca1\u6709\u5bf9\u00a0Foo.X\u00a0\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5219\u9ed8\u8ba4\u662f\u7a7a\u5f15\u7528\uff0c\u53ef\u4ee5\u5229\u7528\u00a0Unsafe.IsNullRef\u00a0\u6765\u5224\u65ad\u4e00\u4e2a\u00a0ref\u00a0\u662f\u5426\u4e3a\u7a7a\uff1a<\/p>\n<pre><code>ref struct Foo\n{    public ref int X;    public bool IsNull =&gt; Unsafe.IsNullRef(ref X);    \n    public Foo(ref int x)\n    {\n        X = ref x;\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u90a3\u5c31\u662f\u00a0ref field\u00a0\u7684\u5b58\u5728\uff0c\u53ef\u80fd\u4f1a\u4f7f\u5f97\u4e00\u4e2a\u00a0ref\u00a0\u6307\u5411\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u88ab\u6269\u5c55\u800c\u5bfc\u81f4\u9519\u8bef\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>Foo MyFancyMethod(){    int x = 1;\n    Foo foo = new(ref x);    return foo; \/\/ error}ref struct Foo\n{    public Foo(ref int x) { }\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u7f16\u8bd1\u65f6\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u00a0foo\u00a0\u5f15\u7528\u4e86\u5c40\u90e8\u53d8\u91cf\u00a0x\uff0c\u800c\u5c40\u90e8\u53d8\u91cf\u00a0x\u00a0\u5728\u51fd\u6570\u8fd4\u56de\u540e\u751f\u547d\u5468\u671f\u5c31\u7ed3\u675f\u4e86\uff0c\u4f46\u662f\u8fd4\u56de\u00a0foo\u00a0\u7684\u64cd\u4f5c\u4f7f\u5f97\u00a0foo\u00a0\u7684\u751f\u547d\u5468\u671f\u6bd4\u00a0x\u00a0\u7684\u751f\u547d\u5468\u671f\u66f4\u957f\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u65e0\u6548\u5f15\u7528\u7684\u95ee\u9898\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u4e86\u8fd9\u4e00\u70b9\uff0c\u4e0d\u5141\u8bb8\u4ee3\u7801\u901a\u8fc7\u7f16\u8bd1\u3002<\/p>\n<p>\u4f46\u662f\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u867d\u7136\u00a0foo\u00a0\u786e\u5b9e\u5f15\u7528\u4e86\u00a0x\uff0c\u4f46\u662f\u00a0foo\u00a0\u5bf9\u8c61\u672c\u8eab\u5e76\u6ca1\u6709\u957f\u671f\u6301\u6709\u00a0x\u00a0\u7684\u5f15\u7528\uff0c\u56e0\u4e3a\u5728\u6784\u9020\u51fd\u6570\u8fd4\u56de\u540e\u5c31\u4e0d\u518d\u6301\u6709\u5bf9\u00a0x\u00a0\u7684\u5f15\u7528\u4e86\uff0c\u56e0\u6b64\u8fd9\u91cc\u6309\u7406\u6765\u8bf4\u4e0d\u5e94\u8be5\u62a5\u9519\u3002\u4e8e\u662f C# 11 \u5f15\u5165\u4e86\u00a0scoped\u00a0\u7684\u6982\u5ff5\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u663e\u5f0f\u6807\u6ce8\u00a0ref\u00a0\u7684\u751f\u547d\u5468\u671f\uff0c\u6807\u6ce8\u4e86\u00a0scoped\u00a0\u7684\u00a0ref\u00a0\u8868\u793a\u8fd9\u4e2a\u5f15\u7528\u7684\u751f\u547d\u5468\u671f\u4e0d\u4f1a\u8d85\u8fc7\u5f53\u524d\u51fd\u6570\u7684\u751f\u547d\u5468\u671f\uff1a<\/p>\n<pre><code>Foo MyFancyMethod(){    int x = 1;\n    Foo foo = new(ref x);    return foo; \/\/ ok}ref struct Foo\n{    public Foo(scoped ref int x) { }\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u7f16\u8bd1\u5668\u5c31\u77e5\u9053\u00a0Foo\u00a0\u7684\u6784\u9020\u51fd\u6570\u4e0d\u4f1a\u4f7f\u5f97\u00a0Foo\u00a0\u5728\u6784\u9020\u51fd\u6570\u8fd4\u56de\u540e\u4ecd\u7136\u6301\u6709\u00a0x\u00a0\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u4e0a\u8ff0\u4ee3\u7801\u5c31\u80fd\u5b89\u5168\u901a\u8fc7\u7f16\u8bd1\u4e86\u3002\u5982\u679c\u6211\u4eec\u8bd5\u56fe\u8ba9\u4e00\u4e2a\u00a0scoped ref\u00a0\u9003\u9038\u51fa\u5f53\u524d\u51fd\u6570\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>ref struct Foo\n{    public ref int X;    public Foo(scoped ref int x)\n    {\n        X = ref x; \/\/ error\n    }\n}<\/code><\/pre>\n<p>\u5982\u6b64\u4e00\u6765\uff0c\u5c31\u5b9e\u73b0\u4e86\u5f15\u7528\u5b89\u5168\u3002<\/p>\n<p>\u5229\u7528\u00a0ref\u00a0\u5b57\u6bb5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u96f6\u5f00\u9500\u8bbe\u65bd\uff0c\u4f8b\u5982\u63d0\u4f9b\u4e00\u4e2a\u591a\u79cd\u65b9\u6cd5\u8bbf\u95ee\u989c\u8272\u6570\u636e\u7684\u00a0ColorView\uff1a<\/p>\n<pre><code>using System.Diagnostics.CodeAnalysis;using System.Runtime.CompilerServices;using System.Runtime.InteropServices;var color = new Color { R = 1, G = 2, B = 3, A = 4 };\ncolor.RawOfU32[0] = 114514;\ncolor.RawOfU16[1] = 19198;\ncolor.RawOfU8[2] = 10;\nConsole.WriteLine(color.A); \/\/ 74[StructLayout(LayoutKind.Explicit)]struct Color\n{\n    [FieldOffset(0)] public byte R;\n    [FieldOffset(1)] public byte G;\n    [FieldOffset(2)] public byte B;\n    [FieldOffset(3)] public byte A;\n\n    [FieldOffset(0)] public uint Rgba;    public ColorView&lt;byte&gt; RawOfU8 =&gt; new(ref this);    public ColorView&lt;ushort&gt; RawOfU16 =&gt; new(ref this);    public ColorView&lt;uint&gt; RawOfU32 =&gt; new(ref this);\n}ref struct ColorView&lt;T&gt; where T : unmanaged{    private ref Color color;    public ColorView(ref Color color)\n    {        this.color = ref color;\n    }\n\n    [DoesNotReturn] private static ref T Throw() =&gt; throw new IndexOutOfRangeException();    public ref T this[uint index]\n    {\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]        get\n        {            unsafe\n            {                return ref (sizeof(T) * index &gt;= sizeof(Color) ?                    ref Throw() :                    ref Unsafe.Add(ref Unsafe.AsRef&lt;T&gt;(Unsafe.AsPointer(ref color)), (int)index));\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u5b57\u6bb5\u4e2d\uff0cref\u00a0\u8fd8\u53ef\u4ee5\u914d\u5408\u00a0readonly\u00a0\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u6765\u8868\u793a\u4e0d\u53ef\u4fee\u6539\u7684\u00a0ref\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>ref int\uff1a\u4e00\u4e2a\u00a0int\u00a0\u7684\u5f15\u7528<\/li>\n<li>readonly ref int\uff1a\u4e00\u4e2a\u00a0int\u00a0\u7684\u53ea\u8bfb\u5f15\u7528<\/li>\n<li>ref readonly int\uff1a\u4e00\u4e2a\u53ea\u8bfb\u00a0int\u00a0\u7684\u5f15\u7528<\/li>\n<li>readonly ref readonly int\uff1a\u4e00\u4e2a\u53ea\u8bfb\u00a0int\u00a0\u7684\u53ea\u8bfb\u5f15\u7528<\/li>\n<\/ul>\n<p>\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u786e\u4fdd\u5f15\u7528\u7684\u5b89\u5168\uff0c\u4f7f\u5f97\u5f15\u7528\u5230\u53ea\u8bfb\u5185\u5bb9\u7684\u5f15\u7528\u4e0d\u4f1a\u88ab\u610f\u5916\u66f4\u6539\u3002<\/p>\n<p>\u5f53\u7136\uff0cC# 11 \u4e2d\u7684\u00a0ref\u00a0\u5b57\u6bb5\u548c\u00a0scoped\u00a0\u652f\u6301\u53ea\u662f\u5176\u5b8c\u5168\u5f62\u6001\u7684\u4e00\u90e8\u5206\uff0c\u66f4\u591a\u7684\u76f8\u5173\u5185\u5bb9\u4ecd\u5728\u8bbe\u8ba1\u548c\u8ba8\u8bba\uff0c\u5e76\u5728\u540e\u7eed\u7248\u672c\u4e2d\u63a8\u51fa\u3002<\/p>\n<h4>\u258c\u6587\u4ef6\u5c40\u90e8\u7c7b\u578b<\/h4>\n<p>C# 11 \u5f15\u5165\u4e86\u65b0\u7684\u6587\u4ef6\u5c40\u90e8\u7c7b\u578b\u53ef\u8bbf\u95ee\u6027\u7b26\u53f7\u00a0file\uff0c\u5229\u7528\u8be5\u53ef\u8bbf\u95ee\u6027\u7b26\u53f7\uff0c\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u53ea\u80fd\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7c7b\u578b\uff1a<\/p>\n<pre><code>\/\/ A.csfile class Foo{    \/\/ ...}\n\nfile struct Bar\n{    \/\/ ...}<\/code><\/pre>\n<p>\u5982\u6b64\u4e00\u6765\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e0e\u00a0Foo\u00a0\u548c\u00a0Bar\u00a0\u7684\u4e0d\u540c\u6587\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e24\u4e2a\u7c7b\u578b\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>\/\/ A.csvar foo = new Foo(); \/\/ okvar bar = new Bar(); \/\/ ok\/\/ B.csvar foo = new Foo(); \/\/ errorvar bar = new Bar(); \/\/ error<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7279\u6027\u5c06\u53ef\u8bbf\u95ee\u6027\u7684\u7c92\u5ea6\u7cbe\u786e\u5230\u4e86\u6587\u4ef6\uff0c\u5bf9\u4e8e\u4ee3\u7801\u751f\u6210\u5668\u7b49\u4e00\u4e9b\u8981\u653e\u5728\u540c\u4e00\u4e2a\u9879\u76ee\u4e2d\uff0c\u4f46\u662f\u53c8\u4e0d\u60f3\u88ab\u5176\u4ed6\u4eba\u63a5\u89e6\u5230\u7684\u4ee3\u7801\u800c\u8a00\u5c06\u4f1a\u7279\u522b\u6709\u7528\u3002<\/p>\n<h4>\u258crequired \u6210\u5458<\/h4>\n<p>C# 11 \u65b0\u589e\u4e86\u00a0required\u00a0\u6210\u5458\uff0c\u6807\u8bb0\u6709\u00a0required\u00a0\u7684\u6210\u5458\u5c06\u4f1a\u88ab\u8981\u6c42\u4f7f\u7528\u65f6\u5fc5\u987b\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>var foo = new Foo(); \/\/ errorvar foo = new Foo { X = 1 }; \/\/ okstruct Foo\n{    public required int X;\n}<\/code><\/pre>\n<p>\u5f00\u53d1\u8005\u8fd8\u53ef\u4ee5\u5229\u7528\u00a0SetsRequiredMembers\u00a0\u8fd9\u4e2a attribute \u6765\u5bf9\u65b9\u6cd5\u8fdb\u884c\u6807\u6ce8\uff0c\u8868\u793a\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u521d\u59cb\u5316\u00a0required\u00a0\u6210\u5458\uff0c\u56e0\u6b64\u7528\u6237\u5728\u4f7f\u7528\u65f6\u53ef\u4ee5\u4e0d\u9700\u8981\u518d\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n<pre><code>using System.Diagnostics.CodeAnalysis;var p = new Point(); \/\/ errorvar p = new Point { X = 1, Y = 2 }; \/\/ okvar p = new Point(1, 2); \/\/ okstruct Point\n{    public required int X;    public required int Y;\n\n    [SetsRequiredMembers]    public Point(int x, int y)\n    {\n        X = x;\n        Y = y;\n    }\n}<\/code><\/pre>\n<p>\u5229\u7528\u00a0required\u00a0\u6210\u5458\uff0c\u6211\u4eec\u53ef\u4ee5\u8981\u6c42\u5176\u4ed6\u5f00\u53d1\u8005\u5728\u4f7f\u7528\u6211\u4eec\u7f16\u5199\u7684\u7c7b\u578b\u65f6\u5fc5\u987b\u521d\u59cb\u5316\u4e00\u4e9b\u6210\u5458\uff0c\u4f7f\u5176\u80fd\u591f\u6b63\u786e\u5730\u4f7f\u7528\u6211\u4eec\u7f16\u5199\u7684\u7c7b\u578b\uff0c\u800c\u4e0d\u4f1a\u5fd8\u8bb0\u521d\u59cb\u5316\u4e00\u4e9b\u6210\u5458\u3002<\/p>\n<h2>\u8fd0\u7b97\u6539\u8fdb<\/h2>\n<h4>\u258cchecked \u8fd0\u7b97\u7b26<\/h4>\n<p>C# \u81ea\u53e4\u4ee5\u6765\u5c31\u6709\u00a0checked\u00a0\u548c\u00a0unchecked\u00a0\u6982\u5ff5\uff0c\u5206\u522b\u8868\u793a\u68c0\u67e5\u548c\u4e0d\u68c0\u67e5\u7b97\u672f\u6ea2\u51fa\uff1a<\/p>\n<pre><code>byte x = 100;byte y = 200;unchecked{    byte z = (byte)(x + y); \/\/ ok}\n\nchecked\n{    byte z = (byte)(x + y); \/\/ error}<\/code><\/pre>\n<p>\u5728 C# 11 \u4e2d\uff0c\u5f15\u5165\u4e86\u00a0checked\u00a0\u8fd0\u7b97\u7b26\u6982\u5ff5\uff0c\u5141\u8bb8\u7528\u6237\u5206\u522b\u5b9e\u73b0\u7528\u4e8e\u00a0checked\u00a0\u548c\u00a0unchecked\u00a0\u7684\u8fd0\u7b97\u7b26\uff1a<\/p>\n<pre><code>struct Foo\n{    public static Foo operator +(Foo left, Foo right) { ... }    public static Foo operator checked +(Foo left, Foo right) { ... }\n}var foo1 = new Foo(...);var foo2 = new Foo(...);var foo3 = unchecked(foo1 + foo2); \/\/ \u8c03\u7528 operator +var foo4 = checked(foo1 + foo2); \/\/ \u8c03\u7528 operator checked +<\/code><\/pre>\n<p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u8fd0\u7b97\u7b26\u800c\u8a00\uff0c\u5b9e\u73b0\u00a0checked\u00a0\u7684\u7248\u672c\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u6ca1\u6709\u5b9e\u73b0\u00a0checked\u00a0\u7684\u7248\u672c\uff0c\u5219\u90fd\u4f1a\u8c03\u7528\u00a0unchecked\u00a0\u7684\u7248\u672c\u3002<\/p>\n<h4>\u258c\u65e0\u7b26\u53f7\u53f3\u79fb\u8fd0\u7b97\u7b26<\/h4>\n<p>C# 11 \u65b0\u589e\u4e86\u00a0&gt;&gt;&gt;\u00a0\u8868\u793a\u65e0\u7b26\u53f7\u7684\u53f3\u79fb\u8fd0\u7b97\u7b26\u3002\u6b64\u524d C# \u7684\u53f3\u79fb\u8fd0\u7b97\u7b26\u00a0&gt;&gt;\u00a0\u9ed8\u8ba4\u662f\u6709\u7b26\u53f7\u7684\u53f3\u79fb\uff0c\u5373\uff1a\u53f3\u79fb\u64cd\u4f5c\u4fdd\u7559\u7b26\u53f7\u4f4d\uff0c\u56e0\u6b64\u5bf9\u4e8e\u00a0int\u00a0\u800c\u8a00\uff0c\u5c06\u4f1a\u6709\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre><code>1 &gt;&gt; 1 = -11 &gt;&gt; 2 = -11 &gt;&gt; 3 = -11 &gt;&gt; 4 = -1\/\/ ...<\/code><\/pre>\n<p>\u800c\u65b0\u7684\u00a0&gt;&gt;&gt;\u00a0\u5219\u662f\u65e0\u7b26\u53f7\u53f3\u79fb\u8fd0\u7b97\u7b26\uff0c\u4f7f\u7528\u540e\u5c06\u4f1a\u6709\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre><code>1 &gt;&gt;&gt; 1 = 21474836471 &gt;&gt;&gt; 2 = 10737418231 &gt;&gt;&gt; 3 = 5368709111 &gt;&gt;&gt; 4 = 268435455\/\/ ...<\/code><\/pre>\n<p>\u8fd9\u7701\u53bb\u4e86\u6211\u4eec\u9700\u8981\u65e0\u7b26\u53f7\u53f3\u79fb\u65f6\uff0c\u9700\u8981\u5148\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u6570\u503c\u540e\u8fdb\u884c\u8ba1\u7b97\uff0c\u518d\u8f6c\u6362\u56de\u6765\u7684\u9ebb\u70e6\uff0c\u4e5f\u80fd\u907f\u514d\u4e0d\u5c11\u56e0\u6b64\u5bfc\u81f4\u7684\u610f\u5916\u9519\u8bef\u3002<\/p>\n<h4>\u258c\u79fb\u4f4d\u8fd0\u7b97\u7b26\u653e\u5f00\u7c7b\u578b\u9650\u5236<\/h4>\n<p>C# 11 \u5f00\u59cb\uff0c\u79fb\u4f4d\u8fd0\u7b97\u7b26\u7684\u53f3\u64cd\u4f5c\u6570\u4e0d\u518d\u8981\u6c42\u5fc5\u987b\u662f\u00a0int\uff0c\u7c7b\u578b\u9650\u5236\u548c\u5176\u4ed6\u8fd0\u7b97\u7b26\u4e00\u6837\u88ab\u653e\u5f00\u4e86\uff0c\u56e0\u6b64\u7ed3\u5408\u4e0a\u9762\u63d0\u5230\u7684\u62bd\u8c61\u548c\u865a\u9759\u6001\u65b9\u6cd5\uff0c\u5141\u8bb8\u6211\u4eec\u58f0\u660e\u6cdb\u578b\u7684\u79fb\u4f4d\u8fd0\u7b97\u7b26\u4e86\uff1a<\/p>\n<pre><code>interface ICanShift&lt;T&gt; where T : ICanShift&lt;T&gt;\n{    abstract static T operator &lt;&lt;(T left, T right);    abstract static T operator &gt;&gt;(T left, T right);\n}<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u4e0a\u8ff0\u7684\u573a\u666f\u662f\u8be5\u9650\u5236\u88ab\u653e\u5f00\u7684\u4e3b\u8981\u76ee\u7684\u3002\u7136\u800c\uff0c\u76f8\u4fe1\u4e0d\u5c11\u8bfb\u8005\u8bfb\u5230\u8fd9\u91cc\u5fc3\u4e2d\u90fd\u53ef\u80fd\u4f1a\u840c\u751f\u4e00\u4e2a\u90aa\u6076\u7684\u60f3\u6cd5\uff0c\u6ca1\u9519\uff0c\u5c31\u662f\u00a0cin\u00a0\u548c\u00a0cout\uff01\u867d\u7136\u8fd9\u79cd\u505a\u6cd5\u5728 C# \u4e2d\u662f\u4e0d\u63a8\u8350\u7684\uff0c\u4f46\u8be5\u9650\u5236\u88ab\u653e\u5f00\u540e\uff0c\u5f00\u53d1\u8005\u786e\u5b9e\u80fd\u7f16\u5199\u7c7b\u4f3c\u7684\u4ee3\u7801\u4e86\uff1a<\/p>\n<pre><code>using static OutStream;using static InStream;int x = 0;\n_ = cin &gt;&gt; To(ref x); \/\/ \u6709 _ = \u662f\u56e0\u4e3a C# \u4e0d\u5141\u8bb8\u8fd0\u7b97\u5f0f\u4e0d\u7ecf\u8fc7\u8d4b\u503c\u800c\u5355\u72ec\u6210\u4e3a\u4e00\u6761\u8bed\u53e5_ = cout &lt;&lt; &quot;hello&quot; &lt;&lt; &quot; &quot; &lt;&lt; &quot;world!&quot;;public class OutStream{    public static OutStream cout = new();    public static OutStream operator &lt;&lt;(OutStream left, string right)\n    {\n        Console.WriteLine(right);        return left;\n    }\n}public class InStream{    public ref struct Ref&lt;T&gt;\n    {        public ref T Value;        public Ref(ref T v) =&gt; Value = ref v;\n    }    public static Ref&lt;T&gt; To&lt;T&gt;(ref T v) =&gt; new (ref v);    public static InStream cin = new();    public static InStream operator &gt;&gt;(InStream left, Ref&lt;int&gt; right)\n    {        var str = Console.Read(...);\n        right.Value = int.Parse(str);\n    }\n}<\/code><\/pre>\n<h4>\u258cIntPtr\u3001UIntPtr \u652f\u6301\u6570\u503c\u8fd0\u7b97<\/h4>\n<p>C# 11 \u4e2d\uff0cIntPtr\u00a0\u548c\u00a0UIntPtr\u00a0\u90fd\u652f\u6301\u6570\u503c\u8fd0\u7b97\u4e86\uff0c\u8fd9\u6781\u5927\u7684\u65b9\u4fbf\u4e86\u6211\u4eec\u5bf9\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre><code>UIntPtr addr = 0x80000048;\nIntPtr offset = 0x00000016;\nUIntPtr newAddr = addr + (UIntPtr)offset; \/\/ 0x8000005E<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u5982\u540c\u00a0Int32\u00a0\u548c\u00a0int\u3001Int64\u00a0\u548c\u00a0long\u00a0\u7684\u5173\u7cfb\u4e00\u6837\uff0cC# \u4e2d\u540c\u6837\u5b58\u5728\u00a0IntPtr\u00a0\u548c\u00a0UIntPtr\u00a0\u7684\u7b49\u4ef7\u7b80\u5199\uff0c\u5206\u522b\u4e3a\u00a0nint\u00a0\u548c\u00a0nuint\uff0cn \u8868\u793a native\uff0c\u7528\u6765\u8868\u793a\u8fd9\u4e2a\u6570\u503c\u7684\u4f4d\u6570\u548c\u5f53\u524d\u8fd0\u884c\u73af\u5883\u7684\u5185\u5b58\u5730\u5740\u4f4d\u6570\u76f8\u540c\uff1a<\/p>\n<pre><code>nuint addr = 0x80000048;nint offset = 0x00000016;nuint newAddr = addr + (nuint)offset; \/\/ 0x8000005E<\/code><\/pre>\n<h2>\u6a21\u5f0f\u5339\u914d\u6539\u8fdb<\/h2>\n<h4>\u258c\u5217\u8868\u6a21\u5f0f\u5339\u914d<\/h4>\n<p>C# 11 \u4e2d\u65b0\u589e\u4e86\u5217\u8868\u6a21\u5f0f\uff0c\u5141\u8bb8\u6211\u4eec\u5bf9\u5217\u8868\u8fdb\u884c\u5339\u914d\u3002\u5728\u5217\u8868\u6a21\u5f0f\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u00a0[ ]\u00a0\u6765\u5305\u62ec\u6211\u4eec\u7684\u6a21\u5f0f\uff0c\u7528\u00a0_\u00a0\u4ee3\u6307\u4e00\u4e2a\u5143\u7d20\uff0c\u7528\u00a0..\u00a0\u4ee3\u8868 0 \u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u3002\u5728\u00a0..\u00a0\u540e\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u7528\u6765\u521b\u5efa\u5339\u914d\u7684\u5b50\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u00a0..\u00a0\u6240\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>var array = new int[] { 1, 2, 3, 4, 5 };if (array is [1, 2, 3, 4, 5]) Console.WriteLine(1); \/\/ 1if (array is [1, 2, 3, ..]) Console.WriteLine(2); \/\/ 2if (array is [1, _, 3, _, 5]) Console.WriteLine(3); \/\/ 3if (array is [.., _, 5]) Console.WriteLine(4); \/\/ 4if (array is [1, 2, 3, .. var remaining])\n{\n    Console.WriteLine(remaining[0]); \/\/ 4\n    Console.WriteLine(remaining.Length); \/\/ 2}<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u548c\u5176\u4ed6\u7684\u6a21\u5f0f\u4e00\u6837\uff0c\u5217\u8868\u6a21\u5f0f\u540c\u6837\u662f\u652f\u6301\u9012\u5f52\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u6a21\u5f0f\u4e0e\u5176\u4ed6\u6a21\u5f0f\u7ec4\u5408\u8d77\u6765\u4f7f\u7528\uff1a<\/p>\n<pre><code>var array = new int[] { 1, 2, 3, 4, 5 };if (array is [1, 2, 3, 4, 5]) Console.WriteLine(1); \/\/ 1if (array is [1, 2, 3, ..]) Console.WriteLine(2); \/\/ 2if (array is [1, _, 3, _, 5]) Console.WriteLine(3); \/\/ 3if (array is [.., _, 5]) Console.WriteLine(4); \/\/ 4if (array is [1, 2, 3, .. var remaining])\n{\n    Console.WriteLine(remaining[0]); \/\/ 4\n    Console.WriteLine(remaining.Length); \/\/ 2}<\/code><\/pre>\n<h4>\u258c\u5bf9 Span&lt;char&gt; \u7684\u6a21\u5f0f\u5339\u914d<\/h4>\n<p>\u5728 C# \u4e2d\uff0cSpan&lt;char&gt;\u00a0\u548c\u00a0ReadOnlySpan&lt;char&gt;\u00a0\u90fd\u53ef\u4ee5\u770b\u4f5c\u662f\u5b57\u7b26\u4e32\u7684\u5207\u7247\uff0c\u56e0\u6b64 C# 11 \u4e5f\u4e3a\u8fd9\u4e24\u4e2a\u7c7b\u578b\u6dfb\u52a0\u4e86\u5b57\u7b26\u4e32\u6a21\u5f0f\u5339\u914d\u7684\u652f\u6301\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>int Foo(ReadOnlySpan&lt;char&gt; span){    if (span is &quot;abcdefg&quot;) return 1;    return 2;\n}\n\nFoo(&quot;abcdefg&quot;.AsSpan()); \/\/ 1Foo(&quot;test&quot;.AsSpan()); \/\/ 2<\/code><\/pre>\n<p>\u5982\u6b64\u4e00\u6765\uff0c\u4f7f\u7528\u00a0Span&lt;char&gt;\u00a0\u6216\u8005\u00a0ReadOnlySpan&lt;char&gt;\u00a0\u7684\u573a\u666f\u4e5f\u80fd\u591f\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\u4e86\uff0c\u800c\u4e0d\u9700\u8981\u5229\u7528\u00a0SequenceEquals\u00a0\u6216\u8005\u7f16\u5199\u5faa\u73af\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h2>\u5b57\u7b26\u4e32\u5904\u7406\u6539\u8fdb<\/h2>\n<h4>\u258c\u539f\u59cb\u5b57\u7b26\u4e32<\/h4>\n<p>C# \u4e2d\u81ea\u521d\u4fbf\u6709\u00a0@\u00a0\u7528\u6765\u8868\u793a\u4e0d\u9700\u8981\u8f6c\u4e49\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u7528\u6237\u8fd8\u662f\u9700\u8981\u5c06\u00a0&#8221;\u00a0\u5199\u6210\u00a0&#8220;&#8221;\u00a0\u624d\u80fd\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5f15\u53f7\u3002C# 11 \u5f15\u5165\u4e86\u539f\u59cb\u5b57\u7b26\u4e32\u7279\u6027\uff0c\u5141\u8bb8\u7528\u6237\u5229\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u5927\u91cf\u7684\u65e0\u9700\u8f6c\u79fb\u7684\u6587\u672c\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5728\u4ee3\u7801\u4e2d\u4ee5\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u585e\u5165\u4ee3\u7801\u6587\u672c\u7b49\u3002<\/p>\n<p>\u539f\u59cb\u5b57\u7b26\u4e32\u9700\u8981\u88ab\u81f3\u5c11\u4e09\u4e2a\u00a0&#8221;\u00a0\u5305\u88f9\uff0c\u4f8b\u5982\u00a0&#8220;&#8221;&#8221;\u00a0\u548c\u00a0&#8220;&#8221;&#8221;&#8221;&#8221;\u00a0\u7b49\u7b49\uff0c\u524d\u540e\u7684\u5f15\u53f7\u6570\u91cf\u8981\u76f8\u7b49\u3002\u53e6\u5916\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u7f29\u8fdb\u7531\u540e\u9762\u5f15\u53f7\u7684\u4f4d\u7f6e\u6765\u786e\u5b9a\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>var str = &quot;&quot;&quot;\n    hello\n    world\n    &quot;&quot;&quot;;<\/code><\/pre>\n<p>\u6b64\u65f6\u00a0str\u00a0\u662f\uff1a<\/p>\n<pre><code>hello\nworld<\/code><\/pre>\n<p>\u800c\u5982\u679c\u662f\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code>var str = &quot;&quot;&quot;\n    hello\n    world\n&quot;&quot;&quot;;<\/code><\/pre>\n<p>str\u00a0\u5219\u4f1a\u6210\u4e3a\uff1a<\/p>\n<pre><code>    hello\n    world<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5728\u4ee3\u7801\u4e2d\u63d2\u5165 JSON \u4ee3\u7801\u4e86\uff1a<\/p>\n<pre><code>var json = &quot;&quot;&quot;\n    {\n        &quot;a&quot;: 1,\n        &quot;b&quot;: {\n            &quot;c&quot;: &quot;hello&quot;,\n            &quot;d&quot;: &quot;world&quot;\n        },\n        &quot;c&quot;: [1, 2, 3, 4, 5]\n    }\n    &quot;&quot;&quot;;\nConsole.WriteLine(json);\/*\n{\n    &quot;a&quot;: 1,\n    &quot;b&quot;: {\n        &quot;c&quot;: &quot;hello&quot;,\n        &quot;d&quot;: &quot;world&quot;\n    },\n    &quot;c&quot;: [1, 2, 3, 4, 5]\n}\n*\/<\/code><\/pre>\n<h4>\u258cUTF-8 \u5b57\u7b26\u4e32<\/h4>\n<p>C# 11 \u5f15\u5165\u4e86 UTF-8 \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u00a0u8\u00a0\u540e\u7f00\u6765\u521b\u5efa\u4e00\u4e2a\u00a0ReadOnlySpan&lt;byte&gt;\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a UTF-8 \u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>var str1 = &quot;hello world&quot;u8; \/\/ ReadOnlySpan&lt;byte&gt;var str2 = &quot;hello world&quot;u8.ToArray(); \/\/ byte[]<\/code><\/pre>\n<p>UTF-8 \u5bf9\u4e8e Web \u573a\u666f\u800c\u8a00\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5728 HTTP \u534f\u8bae\u4e2d\uff0c\u9ed8\u8ba4\u7f16\u7801\u5c31\u662f UTF-8\uff0c\u800c .NET \u5219\u9ed8\u8ba4\u662f UTF-16 \u7f16\u7801\uff0c\u56e0\u6b64\u5728\u5904\u7406 HTTP \u534f\u8bae\u65f6\uff0c\u5982\u679c\u6ca1\u6709 UTF-8 \u5b57\u7b26\u4e32\uff0c\u5219\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684 UTF-8 \u548c UTF-16 \u5b57\u7b26\u4e32\u7684\u76f8\u4e92\u8f6c\u6362\uff0c\u4ece\u800c\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<p>\u6709\u4e86 UTF-8 \u5b57\u7b26\u4e32\u540e\uff0c\u6211\u4eec\u5c31\u80fd\u975e\u5e38\u65b9\u4fbf\u7684\u521b\u5efa UTF-8 \u5b57\u9762\u91cf\u6765\u4f7f\u7528\u4e86\uff0c\u4e0d\u518d\u9700\u8981\u624b\u52a8\u5206\u914d\u4e00\u4e2a\u00a0byte[]\u00a0\u7136\u540e\u5728\u91cc\u9762\u4e00\u4e2a\u4e00\u4e2a\u786c\u7f16\u7801\u6211\u4eec\u9700\u8981\u7684\u5b57\u7b26\u3002<\/p>\n<h4>\u258c\u5b57\u7b26\u4e32\u63d2\u503c\u5141\u8bb8\u6362\u884c<\/h4>\n<p>C# 11 \u5f00\u59cb\uff0c\u5b57\u7b26\u4e32\u7684\u63d2\u503c\u90e8\u5206\u5141\u8bb8\u6362\u884c\uff0c\u56e0\u6b64\u5982\u4e0b\u4ee3\u7801\u53d8\u5f97\u53ef\u80fd\uff1a<\/p>\n<pre><code>var str = $&quot;hello, the leader is {group\n                                    .GetLeader()\n                                    .GetName()}.&quot;;<\/code><\/pre>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u63d2\u503c\u7684\u90e8\u5206\u4ee3\u7801\u5f88\u957f\u65f6\uff0c\u6211\u4eec\u5c31\u80fd\u65b9\u4fbf\u7684\u5bf9\u4ee3\u7801\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u6240\u6709\u4ee3\u7801\u6324\u5728\u4e00\u884c\u3002<\/p>\n<h2>\u5176\u4ed6\u6539\u8fdb<\/h2>\n<h4>\u258cstruct \u81ea\u52a8\u521d\u59cb\u5316<\/h4>\n<p>C# 11 \u5f00\u59cb\uff0cstruct\u00a0\u4e0d\u518d\u5f3a\u5236\u6784\u9020\u51fd\u6570\u5fc5\u987b\u8981\u521d\u59cb\u5316\u6240\u6709\u7684\u5b57\u6bb5\uff0c\u5bf9\u4e8e\u6ca1\u6709\u521d\u59cb\u5316\u7684\u5b57\u6bb5\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u505a\u96f6\u521d\u59cb\u5316\uff1a<\/p>\n<pre><code>struct Point\n{    public int X;    public int Y;    public Point(int x)\n{\n        X = x;        \/\/ Y \u81ea\u52a8\u521d\u59cb\u5316\u4e3a 0\n    }\n}<\/code><\/pre>\n<p>\u258c\u652f\u6301\u5bf9\u5176\u4ed6\u53c2\u6570\u540d\u8fdb\u884c nameof<\/p>\n<p>C# 11 \u5141\u8bb8\u4e86\u5f00\u53d1\u8005\u5728\u53c2\u6570\u4e2d\u5bf9\u5176\u4ed6\u53c2\u6570\u540d\u8fdb\u884c\u00a0nameof\uff0c\u4f8b\u5982\u5728\u4f7f\u7528\u00a0CallerArgumentExpression\u00a0\u8fd9\u4e00 attribute \u65f6\uff0c\u6b64\u524d\u6211\u4eec\u9700\u8981\u76f4\u63a5\u786c\u7f16\u7801\u76f8\u5e94\u53c2\u6570\u540d\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u73b0\u5728\u53ea\u9700\u8981\u4f7f\u7528\u00a0nameof\u00a0\u5373\u53ef\uff1a<\/p>\n<pre><code>void Assert(bool condition, [CallerArgumentExpression(nameof(condition))] string expression = &quot;&quot;)\n{    \/\/ ...}<\/code><\/pre>\n<p>\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u5728\u8fdb\u884c\u4ee3\u7801\u91cd\u6784\u65f6\uff0c\u4fee\u6539\u53c2\u6570\u540d\u00a0condition\u00a0\u65f6\u81ea\u52a8\u4fee\u6539\u00a0nameof\u00a0\u91cc\u9762\u7684\u5185\u5bb9\uff0c\u65b9\u4fbf\u7684\u540c\u65f6\u51cf\u5c11\u51fa\u9519\u3002<\/p>\n<h4>\u258c\u81ea\u52a8\u7f13\u5b58\u9759\u6001\u65b9\u6cd5\u7684\u59d4\u6258<\/h4>\n<p>C# 11 \u5f00\u59cb\uff0c\u4ece\u9759\u6001\u65b9\u6cd5\u521b\u5efa\u7684\u59d4\u6258\u5c06\u4f1a\u88ab\u81ea\u52a8\u7f13\u5b58\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>void Foo(){\n    Call(Console.WriteLine);\n}void Call(Action action){\n    action();\n}<\/code><\/pre>\n<p>\u6b64\u524d\uff0c\u6bcf\u6267\u884c\u4e00\u6b21\u00a0Foo\uff0c\u5c31\u4f1a\u4ece\u00a0Console.WriteLine\u00a0\u8fd9\u4e00\u9759\u6001\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u59d4\u6258\uff0c\u56e0\u6b64\u5982\u679c\u5927\u91cf\u6267\u884c\u00a0Foo\uff0c\u5219\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u59d4\u6258\u88ab\u91cd\u590d\u521b\u5efa\uff0c\u5bfc\u81f4\u5927\u91cf\u7684\u5185\u5b58\u88ab\u5206\u914d\uff0c\u6548\u7387\u6781\u5176\u4f4e\u4e0b\u3002\u5728 C# 11 \u5f00\u59cb\uff0c\u5c06\u4f1a\u81ea\u52a8\u7f13\u5b58\u9759\u6001\u65b9\u6cd5\u7684\u59d4\u6258\uff0c\u56e0\u6b64\u65e0\u8bba\u00a0Foo\u00a0\u88ab\u6267\u884c\u591a\u5c11\u6b21\uff0cConsole.WriteLine\u00a0\u7684\u59d4\u6258\u53ea\u4f1a\u88ab\u521b\u5efa\u4e00\u6b21\uff0c\u8282\u7701\u4e86\u5185\u5b58\u7684\u540c\u65f6\u5927\u5e45\u63d0\u5347\u4e86\u6027\u80fd\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4ece C# 8 \u5f00\u59cb\uff0cC# \u56e2\u961f\u5c31\u5728\u4e0d\u65ad\u5b8c\u5584\u8bed\u8a00\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u5728\u786e\u4fdd\u9759\u6001\u7c7b\u578b\u5b89\u5168\u7684\u540c\u65f6\u5927\u5e45\u63d0\u5347\u8bed\u8a00\u8868\u8fbe\u529b\uff0c\u4ece\u800c\u8ba9\u7c7b\u578b\u7cfb\u7edf\u6210\u4e3a\u7f16\u5199\u7a0b\u5e8f\u7684\u5f97\u529b\u52a9\u624b\uff0c\u800c\u4e0d\u662f\u788d\u624b\u788d\u811a\u7684\u9650\u5236\u3002<\/p>\n<p>\u672c\u6b21\u66f4\u65b0\u8fd8\u5b8c\u5584\u4e86\u6570\u503c\u8fd0\u7b97\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u5229\u7528 C# \u7f16\u5199\u6570\u503c\u8ba1\u7b97\u65b9\u6cd5\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u6a21\u5f0f\u5339\u914d\u7684\u63a2\u7d22\u65c5\u7a0b\u4e5f\u7ec8\u4e8e\u63a5\u8fd1\u5c3e\u58f0\uff0c\u5f15\u5165\u5217\u8868\u6a21\u5f0f\u4e4b\u540e\uff0c\u5269\u4e0b\u7684\u5c31\u53ea\u6709\u5b57\u5178\u6a21\u5f0f\u548c\u6d3b\u52a8\u6a21\u5f0f\u4e86\uff0c\u6a21\u5f0f\u5339\u914d\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5141\u8bb8\u6211\u4eec\u50cf\u5bf9\u5b57\u7b26\u4e32\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u90a3\u6837\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5339\u914d\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4 C# 11 \u7684\u65b0\u7279\u6027\u548c\u6539\u8fdb\u5185\u5bb9\u975e\u5e38\u591a\uff0c\u6bcf\u4e00\u9879\u5185\u5bb9\u90fd\u5bf9 C# \u7684\u4f7f\u7528\u4f53\u9a8c\u6709\u7740\u4e0d\u5c0f\u7684\u63d0\u5347\u3002\u5728\u672a\u6765\u7684 C# \u4e2d\u8fd8\u8ba1\u5212\u7740\u89d2\u8272\u548c\u6269\u5c55\u7b49\u66f4\u52a0\u4ee4\u4eba\u6fc0\u52a8\u7684\u65b0\u7279\u6027\uff0c\u8ba9\u6211\u4eec\u62ed\u76ee\u4ee5\u5f85\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042178695\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\" ><br \/>\u957f\u6309\u8bc6\u522b\u4e8c\u7ef4\u7801\u5173\u6ce8\u5fae\u8f6f\u4e2d\u56fdMSDN\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/p>\n<p>\u70b9\u51fb<a href=\"https:\/\/link.segmentfault.com\/?enc=COJxLu4ocMJUbhTEe0HKfw%3D%3D.nSqm0hvb33OEKTu3wGzc7lPCKytXtEPo54g36pr6XS3hggdK3h6tEVxu0pe2DjuMwVpuun%2F2m6wJSPijwh4%2BxG66lCa26F4J%2FAvnU79eFKYTCYmpu0H3p9QSvZ29YRxf\" rel=\"nofollow\">\u4e86\u89e3C#\u65b0\u589e\u529f\u80fd<\/a>~<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042205595\">https:\/\/segmentfault.com\/a\/1190000042205595<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET 7 \u7684\u5f00\u53d1\u8fd8\u5269\u4e0b\u4e00\u4e2a\u591a\u6708\u5c31\u8981\u8fdb\u5165 RC\uff0cC# 11 \u7684\u65b0\u7279\u6027\u548c\u6539\u8fdb\u4e5f\u5373\u5c06\u6572\u5b9a\u3002\u5728\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\uff0c\u4e0d\u5c11\u65b0 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10005970","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10005970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10005970"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10005970\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10005970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10005970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10005970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}