{"id":182,"date":"2022-12-06T05:28:15","date_gmt":"2022-12-06T13:28:15","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/?p=182"},"modified":"2022-12-06T05:28:15","modified_gmt":"2022-12-06T13:28:15","slug":"%e6%ac%a2%e8%bf%8e%e4%bd%bf%e7%94%a8-c-11","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/%e6%ac%a2%e8%bf%8e%e4%bd%bf%e7%94%a8-c-11\/","title":{"rendered":"\u6b22\u8fce\u4f7f\u7528 C# 11"},"content":{"rendered":"<p><span style=\"font-size: 10pt;\"><strong><em>\u672c\u6587\u7ffb\u8bd1\u4e8eMads Torgersen\u7684\u8fd9\u7bc7\u82f1\u6587\u6587\u7ae0\uff1a<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/welcome-to-csharp-11\/\">Welcome to C# 11 <\/a><\/em><\/strong><\/span>\u3002<\/p>\n<p>\u6211\u5f88\u9ad8\u5174\u5730\u5ba3\u5e03 C# 11 \u5df2\u7ecf\u53d1\u5e03\uff01\u4e0e\u5f80\u5e38\u4e00\u6837\uff0cC# \u5f00\u8f9f\u4e86\u4e00\u4e9b\u5168\u65b0\u7684\u9886\u57df\uff0c\u540c\u65f6\u63a8\u8fdb\u4e86\u8fc7\u53bb\u7248\u672c\u4e2d\u4e00\u76f4\u5728\u8fd0\u884c\u7684\u51e0\u4e2a\u4e3b\u9898\u3002\u6211\u4eec\u7684\u6587\u6863\u9875\u9762\u4e0a\u7684 <a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/whats-new\/csharp-11\">C# 11 \u7684\u65b0\u589e\u529f\u80fd<\/a>\u4e0b\u6709\u8bb8\u591a\u529f\u80fd\u548c\u8be6\u7ec6\u4fe1\u606f\uff0c\u8fd9\u4e9b\u5185\u5bb9\u90fd\u5f97\u5230\u4e86\u5f88\u597d\u7684\u4ecb\u7ecd\u3002 \u63a5\u4e0b\u6765\u662f\u4e00\u4e9b\u4eae\u70b9\u5f00\u80c3\u83dc\u2014\u2014\u6709\u5927\u6709\u5c0f\u3002<\/p>\n<p>\u5728\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u4e4b\u524d\uff0c\u8ba9\u6211\u5148\u8bf4\u4e00\u4e0b\u6211\u5bf9\u8fd9\u4e2a\u7248\u672c\u7684 C# \u7684\u8bde\u751f\u6709\u591a\u9ad8\u5174\uff01\u968f\u7740\u6bcf\u4e2a\u7248\u672c\u7684\u53d1\u5e03\uff0c\u793e\u533a\u7684\u53c2\u4e0e\u5ea6\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ed6\u4eec\u8d21\u732e\u4e86\u4ece\u5efa\u8bae\u3001\u89c1\u89e3\u548cbug\u62a5\u544a\u4e00\u76f4\u5230\u6574\u4e2a\u529f\u80fd\u5b9e\u73b0\u7684\u6240\u6709\u5185\u5bb9\u3002\u8fd9\u771f\u7684\u662f\u6bcf\u4e2a\u4eba\u7684C#\u3002<strong>\u8c22\u8c22\uff01<\/strong><\/p>\n<p><strong>UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC# \u5b57\u7b26\u4e32\u88ab\u786c\u7f16\u7801\u4e3a UTF-16\uff0c\u800c\u4e92\u8054\u7f51\u4e0a\u6d41\u884c\u7684\u5b57\u7b26\u4e32\u7f16\u7801\u662f UTF-8\u3002 \u4e3a\u4e86\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u8f6c\u6362\u7684\u9ebb\u70e6\u548c\u6027\u80fd\u5f00\u9500\uff0c\u60a8\u73b0\u5728\u53ea\u9700\u5c06 u8 \u540e\u7f00\u9644\u52a0\u5230\u5b57\u7b26\u4e32\u6587\u5b57\u5373\u53ef\u7acb\u5373\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a UTF-8\uff1a<\/p>\n<pre class=\"prettyprint\">var u8 = \"This is a UTF-8 string!\"u8;<\/pre>\n<p>UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57\u7b80\u5355\u5730\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u5757\u2014\u2014\u4ee5 ReadOnlySpan&lt;byte&gt; \u7684\u5f62\u5f0f\u3002 \u5bf9\u4e8e UTF-8 \u7f16\u7801\u5f88\u91cd\u8981\u7684\u573a\u666f\uff0c\u8fd9\u53ef\u80fd\u6bd4\u67d0\u4e9b\u4e13\u7528\u7684\u65b0 UTF-8 \u5b57\u7b26\u4e32\u7c7b\u578b\u66f4\u6709\u7528\u3002<\/p>\n<p>\u9605\u8bfb\u6709\u5173 <a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/language-reference\/builtin-types\/reference-types#utf-8-string-literals\">UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57<\/a>\u7684\u6587\u6863\u3002<\/p>\n<p><strong>\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57<\/strong><\/p>\n<p>\u5b57\u7b26\u4e32\u6587\u5b57\u4e2d\u7684\u5f88\u591a\u5185\u5bb9\u90fd\u662f\u67d0\u79cd\u201c\u4ee3\u7801\u201d\u2014\u2014\u4e0d\u4ec5\u662f\u7a0b\u5e8f\u6587\u672c\uff0c\u8fd8\u6709 JSON \u548c XML \u6570\u636e\u3001HTML\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001SQL \u67e5\u8be2\u7b49\u3002 \u5f53\u6709\u8bb8\u591a\u7279\u6b8a\u5b57\u7b26\u5728 C# \u5b57\u7b26\u4e32\u6587\u5b57\u4e2d\u5177\u6709\u7279\u6b8a\u542b\u4e49\u65f6\uff0c\u95ee\u9898\u5c31\u4f1a\u663e\u73b0\uff01 \u503c\u5f97\u6ce8\u610f\u7684\u4f8b\u5b50\u5305\u62ec \\ \u548c &#8220;\uff0c\u5b83\u4eec\u7531 { \u548c } \u8fde\u63a5\u5230\u5185\u63d2\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u91c7\u7528\u4e00\u79cd\u5b8c\u5168\u6ca1\u6709\u8f6c\u4e49\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u6587\u5b57\u5f62\u5f0f\u5462\uff1f \u8fd9\u5c31\u662f\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57\u3002<\/p>\n<p>\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57\u81f3\u5c11\u7531\u4e09\u4e2a\u53cc\u5f15\u53f7\u5206\u9694\uff1a<\/p>\n<pre class=\"prettyprint\">var raw1 = \"\"\"This\\is\\all \"content\"!\"\"\";Console.WriteLine(raw1);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre class=\"prettyprint\">This\\is\\all \"content\"!<\/pre>\n<p>\u5982\u679c\u60a8\u9700\u8981\u4e09\u4e2a\u6216\u66f4\u591a\u7684\u201c\u4f5c\u4e3a\u5185\u5bb9\u7684\u4e00\u90e8\u5206\uff0c\u60a8\u53ea\u9700\u5728\u5916\u90e8\u4f7f\u7528\u66f4\u591a\u7684\u201d\u3002 \u5f00\u5934\u548c\u7ed3\u5c3e\u5fc5\u987b\u5339\u914d\uff1a<\/p>\n<pre class=\"prettyprint\">var raw2 = \"\"\"\"\"I can do \", \"\", \"\"\" or even \"\"\"\" double quotes!\"\"\"\"\";<\/pre>\n<p>\u8fd9\u4f7f\u5f97\u7c98\u8d34\u3001\u7ef4\u62a4\u548c\u9605\u8bfb\u6587\u5b57\u5305\u542b\u7684\u5185\u5bb9\u53d8\u5f97\u975e\u5e38\u5bb9\u6613\u3002<\/p>\n<p>\u591a\u884c\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57\u4e5f\u53ef\u4ee5\u622a\u65ad\u524d\u5bfc\u7a7a\u683c\uff1a\u7ed3\u675f\u5f15\u53f7\u7684\u4f4d\u7f6e\u51b3\u5b9a\u4e86\u8f93\u51fa\u4e2d\u5f00\u59cb\u5305\u542b\u7a7a\u683c\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre class=\"prettyprint\">var raw3 = \"\"\"\r\n    &lt;element attr=\"content\"&gt;\r\n      &lt;body&gt;\r\n        This line is indented by 4 spaces.\r\n      &lt;\/body&gt;\r\n    &lt;\/element&gt;\r\n    \"\"\";\r\n\/\/  ^white space left of here is removed\r\n<\/pre>\n<p>\u7531\u4e8e\u53f3\u5f15\u53f7\u5de6\u4fa7\u6709\u56db\u4e2a\u7a7a\u683c\uff0c\u56e0\u6b64\u4ece\u6bcf\u884c\u5185\u5bb9\u7684\u5f00\u5934\u5220\u9664\u56db\u4e2a\u7a7a\u683c\uff0c\u5bfc\u81f4\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre class=\"prettyprint\">&lt;element attr=\"content\"&gt;\r\n  &lt;body&gt;\r\n    This line is indented by 4 spaces.\r\n  &lt;\/body&gt;\r\n&lt;\/element&gt;\r\n<\/pre>\n<p>\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57\u8fdc\u4e0d\u6b62\u4e8e\u6b64\u2014\u2014\u4f8b\u5982\uff0c\u5b83\u4eec\u652f\u6301\u5b57\u7b26\u4e32\u5185\u63d2\uff01 \u5728\u6587\u6863\u4e2d\u9605\u8bfb\u6709\u5173<a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/programming-guide\/strings\/#raw-string-literals\">\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57<\/a>\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5bf9\u9759\u6001\u6210\u5458\u8fdb\u884c\u62bd\u8c61<\/strong><\/p>\n<p>\u4f60\u5982\u4f55\u62bd\u8c61\u51fa\u672c\u8d28\u4e0a\u662f\u9759\u6001\u7684\u64cd\u4f5c\u2014\u2014\u6bd4\u5982\u8fd0\u7b97\u7b26\uff1f\u4f20\u7edf\u7684\u7b54\u6848\u662f\u201cpoorly\u201d\u3002\u5728 C# 11 \u4e2d\uff0c\u6211\u4eec\u53d1\u5e03\u4e86\u5bf9\u63a5\u53e3\u4e2d\u9759\u6001\u865a\u62df\u6210\u5458\u7684\u652f\u6301\uff0c\u8fd9\u5728 C# 10 \u4e2d\u5904\u4e8e\u9884\u89c8\u72b6\u6001\u3002\u6709\u4e86\u5b83\uff0c\u60a8\u73b0\u5728\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u6570\u5b66\u63a5\u53e3\uff1a<\/p>\n<pre class=\"prettyprint\">public interface IMonoid&lt;TSelf&gt; where TSelf : IMonoid&lt;TSelf&gt;\r\n{\r\n    public static abstract TSelf operator +(TSelf a, TSelf b);\r\n    public static abstract TSelf Zero { get; }\r\n}\r\n<\/pre>\n<p>\u8bf7\u6ce8\u610f\u63a5\u53e3\u5982\u4f55\u4e3a\u201cself\u201d\u83b7\u53d6\u7c7b\u578b\u53c2\u6570\u3002 \u90a3\u662f\u56e0\u4e3a\u9759\u6001\u6210\u5458\u6ca1\u6709this\u3002<\/p>\n<p>\u73b0\u5728\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e3a\u4e24\u4e2a\u9759\u6001\u6210\u5458\u63d0\u4f9b\u5b9e\u73b0\u5e76\u5c06\u5b83\u4eec\u81ea\u5df1\u4f5c\u4e3aTSelf\u7c7b\u578b\u53c2\u6570\u4f20\u9012\u6765\u5b9e\u73b0\u6b64\u63a5\u53e3\uff1a<\/p>\n<pre class=\"prettyprint\">public struct MyInt : IMonoid&lt;MyInt&gt;\r\n{\r\n    int value;\r\n    public MyInt(int i) =&gt; value = i;\r\n    public static MyInt operator +(MyInt a, MyInt b) =&gt; new MyInt(a.value + b.value);\r\n    public static MyInt Zero =&gt; new MyInt(0);\r\n}\r\n<\/pre>\n<p>\u91cd\u8981\u7684\u662f\uff0c\u60a8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u62bd\u8c61\u64cd\u4f5c\uff1f\u5f53\u6ca1\u6709\u5b9e\u4f8b\u53ef\u4ee5\u8c03\u7528\u5b83\u4eec\u65f6\uff0c\u60a8\u5982\u4f55\u8c03\u7528\u865a\u62df\u6210\u5458\uff1f\u7b54\u6848\u662f\u901a\u8fc7\u6cdb\u578b\u3002\u8fd9\u662f\u5b83\u7684\u6837\u5b50\uff1a<\/p>\n<pre class=\"prettyprint\">T AddAll&lt;T&gt;(params T[] elements) where T : IMonoid&lt;T&gt;\r\n{\r\n    T result = T.Zero;\r\n    foreach (var element in elements)\r\n    {\r\n        result += element;\r\n    }\r\n    return result;\r\n}\r\n<\/pre>\n<p>\u7c7b\u578b\u53c2\u6570 T \u53d7 IMonoid&lt;T&gt; \u63a5\u53e3\u7ea6\u675f\uff0c\u8fd9\u5141\u8bb8\u5728 T \u672c\u8eab\u4e0a\u8c03\u7528\u8be5\u63a5\u53e3\u7684\u9759\u6001\u865a\u62df\u6210\u5458\u2014\u2014Zero\u548c +\uff01<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e9b MyInts \u8c03\u7528\u6cdb\u578b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u7c7b\u578b\u53c2\u6570\u4f20\u5165 + \u548c Zero \u7684\u6b63\u786e\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"prettyprint\">MyInt sum = AddAll&lt;MyInt&gt;(new MyInt(3), new MyInt(4), new MyInt(5));\r\n<\/pre>\n<p>\u4e8b\u5b9e\u4e0a\uff0c.NET 7 \u9644\u5e26\u4e86\u4e00\u4e2a\u65b0\u7684\u547d\u540d\u7a7a\u95f4 System.Numerics\uff0c\u5176\u4e2d\u5145\u6ee1\u4e86\u6570\u5b66\u63a5\u53e3\uff0c\u8868\u793a\u60a8\u53ef\u80fd\u60f3\u8981\u4f7f\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u5176\u4ed6\u9759\u6001\u6210\u5458\u7684\u4e0d\u540c\u7ec4\u5408\uff1a IMonoid&lt;T&gt; \u7684\u201c\u6210\u719f\u201d\u7248\u672c \u63a5\u53e3\u5982\u4e0a\u3002 .NET \u4e2d\u7684\u6240\u6709\u6570\u5b57\u7c7b\u578b\u73b0\u5728\u90fd\u5b9e\u73b0\u4e86\u8fd9\u4e9b\u65b0\u63a5\u53e3\u2014\u2014\u60a8\u4e5f\u53ef\u4ee5\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u60a8\u81ea\u5df1\u7684\u7c7b\u578b\u4e2d\uff01 \u56e0\u6b64\uff0c\u73b0\u5728\u5f88\u5bb9\u6613\u4e00\u52b3\u6c38\u9038\u5730\u7f16\u5199\u6570\u503c\u7b97\u6cd5\u2014\u2014\u4ece\u5b83\u4eec\u5904\u7406\u7684\u5177\u4f53\u7c7b\u578b\u4e2d\u62bd\u8c61\u51fa\u6765\u2014\u2014\u800c\u4e0d\u662f\u8ba9\u5927\u91cf\u7684\u91cd\u8f7d\u5305\u542b\u672c\u8d28\u4e0a\u76f8\u540c\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u8fd8\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u9759\u6001\u865a\u62df\u6210\u5458\u5bf9\u4e8e\u6570\u5b66\u4ee5\u5916\u7684\u5176\u4ed6\u4e8b\u60c5\u4e5f\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5bf9\u7c7b\u578b\u5c42\u6b21\u7ed3\u6784\u7684\u5de5\u5382\u65b9\u6cd5\u8fdb\u884c\u62bd\u8c61\u3002\u4f46\u6211\u4eec\u73b0\u5728\u5df2\u7ecf\u6db5\u76d6\u4e86\u8db3\u591f\u591a\u7684\u5185\u5bb9\u2014\u2014\u60a8\u53ef\u80fd\u60f3\u67e5\u770b\u6587\u6863\u4e2d\u5173\u4e8e<a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/whats-new\/tutorials\/static-virtual-interface-members#static-abstract-interface-methods\">\u9759\u6001\u62bd\u8c61\u63a5\u53e3\u65b9\u6cd5<\/a>\u548c<a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/whats-new\/tutorials\/static-virtual-interface-members#generic-math\">\u6cdb\u578b\u6570\u5b66<\/a>\u7684\u8fd9\u4e9b\u6559\u7a0b\u3002<\/p>\n<p>\u5373\u4f7f\u60a8\u4e0d\u4f7f\u7528\u9759\u6001\u865a\u62df\u6210\u5458\u521b\u5efa\u63a5\u53e3\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4ece\u73b0\u5728\u548c\u5c06\u6765\u5bf9 .NET \u5e93\u6240\u505a\u7684\u6539\u8fdb\u4e2d\u53d7\u76ca\u3002<\/p>\n<p><strong>\u5217\u8868\u6a21\u5f0f<\/strong><\/p>\n<p>\u6a21\u5f0f\u5339\u914d\u662f C# \u4e2d\u6b63\u5728\u8fdb\u884c\u7684\u6545\u4e8b\u4e4b\u4e00\uff0c\u6211\u4eec\u53ea\u662f\u4e0d\u65ad\u5730\u586b\u5145\u5b83\u3002\u6a21\u5f0f\u5339\u914d\u662f\u5728 C# 7 \u4e2d\u5f15\u5165\u7684\uff0c\u4ece\u90a3\u65f6\u8d77\u5b83\u5df2\u7ecf\u53d1\u5c55\u6210\u4e3a\u8be5\u8bed\u8a00\u4e2d\u6700\u91cd\u8981\u548c\u6700\u5f3a\u5927\u7684\u63a7\u5236\u7ed3\u6784\u4e4b\u4e00\u3002<\/p>\n<p>C# 11 \u5c06\u5217\u8868\u6a21\u5f0f\u6dfb\u52a0\u5230\u6545\u4e8b\u4e2d\u3002 \u4f7f\u7528\u5217\u8868\u6a21\u5f0f\uff0c\u60a8\u53ef\u4ee5\u9012\u5f52\u5730\u5c06\u6a21\u5f0f\u5e94\u7528\u4e8e\u7c7b\u4f3c\u5217\u8868\u7684\u8f93\u5165\u7684\u5355\u4e2a\u5143\u7d20\u2014\u2014\u6216\u8005\u5b83\u4eec\u7684\u8303\u56f4\u3002 \u8ba9\u6211\u4eec\u76f4\u63a5\u8fdb\u5165\u4e0a\u9762\u7684\u901a\u7528\u7b97\u6cd5\uff0c\u4f7f\u7528\u5217\u8868\u6a21\u5f0f\u91cd\u5199\u4e3a\u9012\u5f52\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"prettyprint\">T AddAll&lt;T&gt;(params T[] elements) where T : IMonoid&lt;T&gt; =&gt; \r\n    elements switch\r\n{\r\n    [] =&gt; T.Zero,\r\n    [var first, ..var rest] =&gt; first + AddAll&lt;T&gt;(rest),\r\n};\r\n<\/pre>\n<p>\u91cc\u9762\u8fdb\u884c\u4e86\u5f88\u591a\u4e8b\u60c5\uff0c\u4f46\u5728\u4e2d\u5fc3\u662f\u4e00\u4e2a\u5e26\u6709\u4e24\u79cd\u60c5\u51b5\u7684 switch \u8868\u8fbe\u5f0f\u3002 \u4e00\u79cd\u60c5\u51b5\u4e3a\u7a7a\u5217\u8868 [] \u8fd4\u56de\u96f6\uff0c\u5176\u4e2dZero\u7531\u63a5\u53e3\u5b9a\u4e49\u3002 \u53e6\u4e00\u79cd\u60c5\u51b5\u4f7f\u7528 var first \u6a21\u5f0f\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u63d0\u53d6\u5230 first \u4e2d\uff0c\u7136\u540e\u4f7f\u7528 .. \u5c06\u5269\u4f59\u5143\u7d20\u63d0\u53d6\u5230 rest \u4e2d\uff0c\u4ee5\u5c06\u6240\u6709\u5269\u4f59\u5143\u7d20\u5207\u51fa\u5230 var rest \u6a21\u5f0f\u4e2d\u3002<\/p>\n<p>\u5728\u6587\u6863\u4e2d\u9605\u8bfb\u6709\u5173<a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/language-reference\/operators\/patterns#list-patterns\">\u5217\u8868\u6a21\u5f0f<\/a>\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5fc5\u9700\u7684\u6210\u5458<\/strong><\/p>\n<p>\u6211\u4eec\u4e00\u76f4\u81f4\u529b\u4e8e\u591a\u4e2a\u7248\u672c\u7684\u53e6\u4e00\u4e2a\u6301\u7eed\u4e3b\u9898\u662f\u6539\u8fdb\u5bf9\u8c61\u521b\u5efa\u548c\u521d\u59cb\u5316\u3002C# 11 \u7ee7\u7eed\u5bf9\u5fc5\u9700\u7684\u6210\u5458\u8fdb\u884c\u8fd9\u4e9b\u6539\u8fdb\u3002<\/p>\n<p>\u5728\u521b\u5efa\u4f7f\u7528\u5bf9\u8c61\u521d\u59cb\u503c\u8bbe\u5b9a\u9879\u7684\u7c7b\u578b\u65f6\uff0c\u60a8\u8fc7\u53bb\u65e0\u6cd5\u6307\u5b9a\u5fc5\u987b\u521d\u59cb\u5316\u67d0\u4e9b\u5c5e\u6027\u3002\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u8bf4\u5c5e\u6027\u6216\u5b57\u6bb5\u662f\u5fc5\u9700\u7684\u3002 \u8fd9\u610f\u5473\u7740\u5f53\u521b\u5efa\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u5b83\u5fc5\u987b\u7531\u5bf9\u8c61\u521d\u59cb\u503c\u8bbe\u5b9a\u9879\u521d\u59cb\u5316\uff1a<\/p>\n<pre class=\"prettyprint\">public class Person\r\n\r\n{\r\n\r\n\u00a0\u00a0\u00a0 public required string FirstName { get; init; }\r\n\r\n\u00a0\u00a0\u00a0 public string? MiddleName { get; init; }\r\n\r\n\u00a0\u00a0\u00a0 public required string LastName { get; init; }\r\n\r\n}<\/pre>\n<p>\u73b0\u5728\u5728\u6ca1\u6709\u521d\u59cb\u5316\u4e24\u4e2a\u5fc5\u9700\u5c5e\u6027\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u4e00\u4e2a Person \u662f\u9519\u8bef\u7684\uff1a<\/p>\n<pre class=\"prettyprint\">var person = new Person { FirstName = \"Ada\" }; \/\/ Error: no LastName!<\/pre>\n<p>\u67e5\u770b\u6587\u6863\u4ee5\u83b7\u53d6\u6709\u5173<a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/language-reference\/keywords\/required\">\u6240\u9700\u6210\u5458<\/a>\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6700\u540e<\/strong><\/p>\n<p>C# 11 \u8fd8\u5305\u62ec\u8bb8\u591a\u5176\u4ed6\u529f\u80fd\u3002 \u6211\u5e0c\u671b\u8fd9\u9053\u5f00\u80c3\u83dc\u80fd\u6fc0\u53d1\u60a8\u63a2\u7d22 <a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/whats-new\/csharp-11\">C# 11 \u4e2d\u7684\u65b0\u529f\u80fd<\/a>\uff0c\u5e76\u5e0c\u671b\u60a8\u5728\u4f7f\u7528 C# 11 \u8fdb\u884c\u7f16\u7801\u65f6\u83b7\u5f97\u4e0e\u6211\u4eec\u7f16\u5199\u5b83\u65f6\u4e00\u6837\u591a\u7684\u4e50\u8da3\uff01\u6211\u4eec\u52aa\u529b\u4f7f\u8bed\u8a00\u53d8\u5f97\u66f4\u6709\u7528\uff0c\u4e0d\u4ec5\u901a\u8fc7\u589e\u52a0\u66f4\u591a\u7684\u8868\u8fbe\u80fd\u529b\uff08\u5982\u9759\u6001\u865a\u62df\u6210\u5458\uff09\uff0c\u800c\u4e14\u901a\u8fc7\u7b80\u5316\u3001\u7cbe\u7b80\u548c\u5220\u9664\u6837\u677f\uff08\u5982\u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57\u548c\u5217\u8868\u6a21\u5f0f\uff09\u5e76\u4f7f\u5176\u66f4\u5b89\u5168\u3002<\/p>\n<p>\u611f\u8c22\u60a8\u4f7f\u7528C# 11.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7ffb\u8bd1\u4e8eMads Torgersen\u7684\u8fd9\u7bc7\u82f1\u6587\u6587\u7ae0\uff1aWelcome to C# 11 \u3002 \u6211\u5f88\u9ad8\u5174\u5730\u5ba3\u5e03 C# 11 \u5df2\u7ecf\u53d1\u5e03\uff01\u4e0e\u5f80\u5e38\u4e00\u6837\uff0cC# \u5f00\u8f9f\u4e86\u4e00\u4e9b\u5168\u65b0\u7684\u9886\u57df\uff0c\u540c\u65f6\u63a8\u8fdb\u4e86\u8fc7\u53bb\u7248\u672c\u4e2d\u4e00\u76f4\u5728\u8fd0\u884c\u7684\u51e0\u4e2a\u4e3b\u9898\u3002\u6211\u4eec\u7684\u6587\u6863\u9875\u9762\u4e0a\u7684 C# 11 \u7684\u65b0\u589e\u529f\u80fd\u4e0b\u6709\u8bb8\u591a\u529f\u80fd\u548c\u8be6\u7ec6\u4fe1\u606f\uff0c\u8fd9\u4e9b\u5185\u5bb9\u90fd\u5f97\u5230\u4e86\u5f88\u597d\u7684\u4ecb\u7ecd\u3002 \u63a5\u4e0b\u6765\u662f\u4e00\u4e9b\u4eae\u70b9\u5f00\u80c3\u83dc\u2014\u2014\u6709\u5927\u6709\u5c0f\u3002 \u5728\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u4e4b\u524d\uff0c\u8ba9\u6211\u5148\u8bf4\u4e00\u4e0b\u6211\u5bf9\u8fd9\u4e2a\u7248\u672c\u7684 C# \u7684\u8bde\u751f\u6709\u591a\u9ad8\u5174\uff01\u968f\u7740\u6bcf\u4e2a\u7248\u672c\u7684\u53d1\u5e03\uff0c\u793e\u533a\u7684\u53c2\u4e0e\u5ea6\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ed6\u4eec\u8d21\u732e\u4e86\u4ece\u5efa\u8bae\u3001\u89c1\u89e3\u548cbug\u62a5\u544a\u4e00\u76f4\u5230\u6574\u4e2a\u529f\u80fd\u5b9e\u73b0\u7684\u6240\u6709\u5185\u5bb9\u3002\u8fd9\u771f\u7684\u662f\u6bcf\u4e2a\u4eba\u7684C#\u3002\u8c22\u8c22\uff01 UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC# \u5b57\u7b26\u4e32\u88ab\u786c\u7f16\u7801\u4e3a UTF-16\uff0c\u800c\u4e92\u8054\u7f51\u4e0a\u6d41\u884c\u7684\u5b57\u7b26\u4e32\u7f16\u7801\u662f UTF-8\u3002 \u4e3a\u4e86\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u8f6c\u6362\u7684\u9ebb\u70e6\u548c\u6027\u80fd\u5f00\u9500\uff0c\u60a8\u73b0\u5728\u53ea\u9700\u5c06 u8 \u540e\u7f00\u9644\u52a0\u5230\u5b57\u7b26\u4e32\u6587\u5b57\u5373\u53ef\u7acb\u5373\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a UTF-8\uff1a var u8 = &#8220;This is a UTF-8 string!&#8221;u8; UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57\u7b80\u5355\u5730\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u5757\u2014\u2014\u4ee5 ReadOnlySpan&lt;byte&gt; \u7684\u5f62\u5f0f\u3002 \u5bf9\u4e8e UTF-8 \u7f16\u7801\u5f88\u91cd\u8981\u7684\u573a\u666f\uff0c\u8fd9\u53ef\u80fd\u6bd4\u67d0\u4e9b\u4e13\u7528\u7684\u65b0 UTF-8 \u5b57\u7b26\u4e32\u7c7b\u578b\u66f4\u6709\u7528\u3002 \u9605\u8bfb\u6709\u5173 UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57\u7684\u6587\u6863\u3002 \u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57 \u5b57\u7b26\u4e32\u6587\u5b57\u4e2d\u7684\u5f88\u591a\u5185\u5bb9\u90fd\u662f\u67d0\u79cd\u201c\u4ee3\u7801\u201d\u2014\u2014\u4e0d\u4ec5\u662f\u7a0b\u5e8f\u6587\u672c\uff0c\u8fd8\u6709 JSON \u548c XML \u6570\u636e\u3001HTML\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001SQL \u67e5\u8be2\u7b49\u3002 [&hellip;]<\/p>\n","protected":false},"author":99475,"featured_media":183,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,10],"class_list":["post-182","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet-ch","tag-net","tag-c"],"acf":[],"blog_post_summary":"<p>\u672c\u6587\u7ffb\u8bd1\u4e8eMads Torgersen\u7684\u8fd9\u7bc7\u82f1\u6587\u6587\u7ae0\uff1aWelcome to C# 11 \u3002 \u6211\u5f88\u9ad8\u5174\u5730\u5ba3\u5e03 C# 11 \u5df2\u7ecf\u53d1\u5e03\uff01\u4e0e\u5f80\u5e38\u4e00\u6837\uff0cC# \u5f00\u8f9f\u4e86\u4e00\u4e9b\u5168\u65b0\u7684\u9886\u57df\uff0c\u540c\u65f6\u63a8\u8fdb\u4e86\u8fc7\u53bb\u7248\u672c\u4e2d\u4e00\u76f4\u5728\u8fd0\u884c\u7684\u51e0\u4e2a\u4e3b\u9898\u3002\u6211\u4eec\u7684\u6587\u6863\u9875\u9762\u4e0a\u7684 C# 11 \u7684\u65b0\u589e\u529f\u80fd\u4e0b\u6709\u8bb8\u591a\u529f\u80fd\u548c\u8be6\u7ec6\u4fe1\u606f\uff0c\u8fd9\u4e9b\u5185\u5bb9\u90fd\u5f97\u5230\u4e86\u5f88\u597d\u7684\u4ecb\u7ecd\u3002 \u63a5\u4e0b\u6765\u662f\u4e00\u4e9b\u4eae\u70b9\u5f00\u80c3\u83dc\u2014\u2014\u6709\u5927\u6709\u5c0f\u3002 \u5728\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u4e4b\u524d\uff0c\u8ba9\u6211\u5148\u8bf4\u4e00\u4e0b\u6211\u5bf9\u8fd9\u4e2a\u7248\u672c\u7684 C# \u7684\u8bde\u751f\u6709\u591a\u9ad8\u5174\uff01\u968f\u7740\u6bcf\u4e2a\u7248\u672c\u7684\u53d1\u5e03\uff0c\u793e\u533a\u7684\u53c2\u4e0e\u5ea6\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ed6\u4eec\u8d21\u732e\u4e86\u4ece\u5efa\u8bae\u3001\u89c1\u89e3\u548cbug\u62a5\u544a\u4e00\u76f4\u5230\u6574\u4e2a\u529f\u80fd\u5b9e\u73b0\u7684\u6240\u6709\u5185\u5bb9\u3002\u8fd9\u771f\u7684\u662f\u6bcf\u4e2a\u4eba\u7684C#\u3002\u8c22\u8c22\uff01 UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC# \u5b57\u7b26\u4e32\u88ab\u786c\u7f16\u7801\u4e3a UTF-16\uff0c\u800c\u4e92\u8054\u7f51\u4e0a\u6d41\u884c\u7684\u5b57\u7b26\u4e32\u7f16\u7801\u662f UTF-8\u3002 \u4e3a\u4e86\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u8f6c\u6362\u7684\u9ebb\u70e6\u548c\u6027\u80fd\u5f00\u9500\uff0c\u60a8\u73b0\u5728\u53ea\u9700\u5c06 u8 \u540e\u7f00\u9644\u52a0\u5230\u5b57\u7b26\u4e32\u6587\u5b57\u5373\u53ef\u7acb\u5373\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a UTF-8\uff1a var u8 = &#8220;This is a UTF-8 string!&#8221;u8; UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57\u7b80\u5355\u5730\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u5757\u2014\u2014\u4ee5 ReadOnlySpan&lt;byte&gt; \u7684\u5f62\u5f0f\u3002 \u5bf9\u4e8e UTF-8 \u7f16\u7801\u5f88\u91cd\u8981\u7684\u573a\u666f\uff0c\u8fd9\u53ef\u80fd\u6bd4\u67d0\u4e9b\u4e13\u7528\u7684\u65b0 UTF-8 \u5b57\u7b26\u4e32\u7c7b\u578b\u66f4\u6709\u7528\u3002 \u9605\u8bfb\u6709\u5173 UTF-8 \u5b57\u7b26\u4e32\u6587\u5b57\u7684\u6587\u6863\u3002 \u539f\u59cb\u5b57\u7b26\u4e32\u6587\u5b57 \u5b57\u7b26\u4e32\u6587\u5b57\u4e2d\u7684\u5f88\u591a\u5185\u5bb9\u90fd\u662f\u67d0\u79cd\u201c\u4ee3\u7801\u201d\u2014\u2014\u4e0d\u4ec5\u662f\u7a0b\u5e8f\u6587\u672c\uff0c\u8fd8\u6709 JSON \u548c XML \u6570\u636e\u3001HTML\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001SQL \u67e5\u8be2\u7b49\u3002 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/users\/99475"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/media\/183"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}