{"id":3574,"date":"2025-04-19T10:25:56","date_gmt":"2025-04-19T02:25:56","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3574"},"modified":"2025-04-19T10:25:56","modified_gmt":"2025-04-19T02:25:56","slug":"readonly-indexers-ref-returns-span-%e9%85%8d%e5%90%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/readonly-indexers-ref-returns-span-%e9%85%8d%e5%90%88%e4%bd%bf%e7%94%a8\/","title":{"rendered":"readonly indexers, ref returns, Span<t> \u914d\u5408\u4f7f\u7528<\/t>"},"content":{"rendered":"\n<p>\u5728 C# \u4e2d\uff0c\u7d22\u5f15\u5668\uff08Indexer\uff09\u4e0d\u4ec5\u53ef\u4ee5\u7b80\u5355\u5730\u7528\u6765\u5c01\u88c5\u6570\u7ec4\u6216\u5217\u8868\u7684\u8bbf\u95ee\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff0c\u5982 <strong><code>readonly indexers<\/code><\/strong>\u3001<strong><code>ref returns<\/code><\/strong> \u548c <strong><code>Span&lt;T><\/code><\/strong> \u6765\u4f18\u5316\u6027\u80fd\u6216\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002\u4e0b\u9762\u9010\u4e00\u4ecb\u7ecd\u8fd9\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>Readonly Indexers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4ecb\u7ecd\uff1a<\/h3>\n\n\n\n<p><code>readonly<\/code> \u7d22\u5f15\u5668\u610f\u5473\u7740\u7d22\u5f15\u5668\u7684\u503c\u662f\u53ea\u8bfb\u7684\uff0c\u65e0\u6cd5\u901a\u8fc7\u7d22\u5f15\u5668\u6765\u4fee\u6539\u5bf9\u8c61\u7684\u5185\u5bb9\u3002\u8fd9\u79cd\u7528\u6cd5\u9002\u7528\u4e8e\u53ea\u9700\u8981\u8bbf\u95ee\u6570\u636e\u800c\u4e0d\u4fee\u6539\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a<code>readonly<\/code> \u7d22\u5f15\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ReadOnlyCollection&lt;T&gt;\n{\n    private readonly List&lt;T&gt; items;\n\n    public ReadOnlyCollection(IEnumerable&lt;T&gt; collection)\n    {\n        items = new List&lt;T&gt;(collection);\n    }\n\n    \/\/ \u53ea\u8bfb\u7d22\u5f15\u5668\uff0c\u65e0\u6cd5\u4fee\u6539\u5143\u7d20\n    public T this&#91;int index] =&gt; items&#91;index];\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var collection = new ReadOnlyCollection&lt;int&gt;(new&#91;] { 1, 2, 3, 4 });\nConsole.WriteLine(collection&#91;2]); \/\/ \u8f93\u51fa 3\n\/\/ collection&#91;2] = 5; \/\/ \u7f16\u8bd1\u9519\u8bef\uff0c\u65e0\u6cd5\u4fee\u6539<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u8bfb\u7d22\u5f15\u5668\u5728\u9700\u8981\u4fdd\u62a4\u6570\u636e\u4e0d\u88ab\u4fee\u6539\u65f6\u7279\u522b\u6709\u7528\uff0c\u6bd4\u5982\u8fd4\u56de\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u65f6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>Ref Returns \u4e0e Indexers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4ecb\u7ecd\uff1a<\/h3>\n\n\n\n<p><strong><code>ref returns<\/code><\/strong> \u5141\u8bb8\u7d22\u5f15\u5668\u8fd4\u56de\u6570\u636e\u7684\u5f15\u7528\uff08<code>ref<\/code>\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u5185\u90e8\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u5185\u5b58\u62f7\u8d1d\u3002\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u5b58\u50a8\u5728\u8f83\u5927\u7684\u96c6\u5408\u4e2d\u65f6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a\u4f7f\u7528 <code>ref<\/code> \u8fd4\u56de\u503c\u7684\u7d22\u5f15\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class RefReturnCollection\n{\n    private int&#91;] data = new int&#91;10];\n\n    public ref int this&#91;int index]\n    {\n        get =&gt; ref data&#91;index];\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var collection = new RefReturnCollection();\ncollection&#91;0] = 42;\nConsole.WriteLine(collection&#91;0]); \/\/ \u8f93\u51fa 42\n\n\/\/ \u76f4\u63a5\u901a\u8fc7 ref \u4fee\u6539\nref int value = ref collection&#91;0];\nvalue = 99;\nConsole.WriteLine(collection&#91;0]); \/\/ \u8f93\u51fa 99<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>ref<\/code> \u8fd4\u56de\u503c\u53ef\u4ee5\u5728\u4e0d\u590d\u5236\u6570\u636e\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u4fee\u6539\u5bf9\u8c61\u7684\u5185\u90e8\u72b6\u6001\u3002<\/li>\n\n\n\n<li>\u9002\u7528\u4e8e\u5728\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u5982\u4fee\u6539\u5927\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>Span&lt;T><\/code> \u4e0e Indexers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4ecb\u7ecd\uff1a<\/h3>\n\n\n\n<p><code>Span&lt;T&gt;<\/code> \u662f C# \u4e2d\u7684\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7c7b\u578b\uff0c\u5b83\u63d0\u4f9b\u5bf9\u5185\u5b58\u7247\u6bb5\u7684\u9ad8\u6548\u8bbf\u95ee\u3002\u5728\u4f7f\u7528\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230\u5207\u7247\u64cd\u4f5c\u65f6\uff0c<code>Span&lt;T&gt;<\/code> \u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5206\u914d\uff0c\u5e76\u4e14\u80fd\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a\u7ed3\u5408 <code>Span&lt;T&gt;<\/code> \u4e0e\u7d22\u5f15\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SpanIndexer\n{\n    private int&#91;] data = new int&#91;100];\n\n    public Span&lt;int&gt; this&#91;int start, int length]\n    {\n        get =&gt; new Span&lt;int&gt;(data, start, length);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var spanIndexer = new SpanIndexer();\nvar span = spanIndexer&#91;10, 5];\nspan&#91;0] = 42; \/\/ \u4fee\u6539 span \u4e2d\u7684\u503c\n\nConsole.WriteLine(span&#91;0]); \/\/ \u8f93\u51fa 42<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Span&lt;T><\/code> \u53ef\u4ee5\u5e2e\u52a9\u4f60\u907f\u514d\u5bf9\u6570\u7ec4\u6216\u5217\u8868\u7684\u590d\u5236\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u6216\u8005\u9700\u8981\u5bf9\u6570\u7ec4\u4e2d\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u5904\u7406\u65f6\u3002<\/li>\n\n\n\n<li>\u8bf7\u6ce8\u610f\uff0c<code>Span&lt;T><\/code> \u4e0d\u80fd\u5728\u5806\u4e0a\u5206\u914d\uff0c\u9002\u7528\u4e8e\u6808\u4e0a\u6570\u636e\uff08\u5982\u5c40\u90e8\u53d8\u91cf\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u5b9e\u9645\u6027\u80fd\u4f18\u5316<\/strong><\/h2>\n\n\n\n<p>\u7ed3\u5408 <strong><code>ref returns<\/code><\/strong> \u548c <strong><code>Span&lt;T&gt;<\/code><\/strong>\uff0c\u53ef\u4ee5\u5927\u5e45\u4f18\u5316\u6570\u7ec4\u6216\u96c6\u5408\u7684\u64cd\u4f5c\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u590d\u5236\u4e0e\u5185\u5b58\u5206\u914d\uff0c\u4ece\u800c\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c<strong><code>ref<\/code><\/strong> \u548c <strong><code>Span&lt;T&gt;<\/code><\/strong> \u80fd\u663e\u8457\u51cf\u5c11\u8d44\u6e90\u6d88\u8017\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a\u6027\u80fd\u4f18\u5316\u7684\u96c6\u5408\u64cd\u4f5c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class OptimizedCollection\n{\n    private int&#91;] data = new int&#91;10000];\n\n    \/\/ \u4f7f\u7528 ref return \u6765\u8fd4\u56de\u6570\u636e\u5f15\u7528\n    public ref int this&#91;int index]\n    {\n        get =&gt; ref data&#91;index];\n    }\n\n    \/\/ \u4f7f\u7528 Span \u6765\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\n    public Span&lt;int&gt; Slice(int start, int length)\n    {\n        return new Span&lt;int&gt;(data, start, length);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var collection = new OptimizedCollection();\nref int value = ref collection&#91;100];  \/\/ \u83b7\u53d6\u5f15\u7528\nvalue = 999;  \/\/ \u4fee\u6539\u6570\u636e\n\nvar slice = collection.Slice(50, 20);  \/\/ \u83b7\u53d6\u6570\u636e\u7247\u6bb5\nslice&#91;0] = 123; \/\/ \u4fee\u6539\u6570\u636e\u7247\u6bb5\nConsole.WriteLine(slice&#91;0]);  \/\/ \u8f93\u51fa 123<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7528\u6cd5<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>readonly indexers<\/code><\/td><td>\u6570\u636e\u4e0d\u5e94\u88ab\u4fee\u6539\u65f6\uff0c\u4fdd\u62a4\u6570\u636e<\/td><td>\u7528\u4e8e\u8fd4\u56de\u6570\u636e\u800c\u4e0d\u5141\u8bb8\u4fee\u6539\uff0c\u5982\u8fd4\u56de\u4e0d\u53ef\u53d8\u96c6\u5408<\/td><\/tr><tr><td><code>ref returns<\/code><\/td><td>\u9ad8\u6027\u80fd\u4fee\u6539\u6570\u636e\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d<\/td><td>\u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u5f15\u7528\u4fee\u6539\uff0c\u9002\u5408\u5927\u6570\u636e\u91cf\u64cd\u4f5c<\/td><\/tr><tr><td><code>Span&lt;T&gt;<\/code><\/td><td>\u5927\u6570\u636e\u96c6\u5207\u7247\u64cd\u4f5c\uff0c\u907f\u514d\u5185\u5b58\u5206\u914d<\/td><td>\u63d0\u4f9b\u5bf9\u5185\u5b58\u7247\u6bb5\u7684\u9ad8\u6548\u8bbf\u95ee\uff0c\u907f\u514d\u6570\u636e\u590d\u5236<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ref returns<\/code><\/strong> \u4e0e <strong><code>Span&lt;T><\/code><\/strong> \u901a\u5e38\u914d\u5408\u4f7f\u7528\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u590d\u5236\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u6216\u8fdb\u884c\u9ad8\u6548\u5185\u5b58\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <strong><code>readonly indexers<\/code><\/strong> \u6765\u5c01\u88c5\u6570\u636e\u5e76\u4fdd\u62a4\u5176\u4e0d\u88ab\u4fee\u6539\uff0c\u7279\u522b\u9002\u5408\u4e8e\u4e0d\u53ef\u53d8\u5bf9\u8c61\u6216\u53ea\u8bfb\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u6743\u5a01\u53c2\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/indexers\/\">C# Indexers &#8211; Microsoft Learn<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.span-1?view=net-7.0\">C# Span &#8211; Microsoft Learn<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/ref-returns\">C# Ref Returns &#8211; Microsoft Learn<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u6709\u52a9\u4e8e\u4f60\u5728\u66f4\u590d\u6742\u7684\u573a\u666f\u4e0b\u5b9e\u73b0\u66f4\u9ad8\u6548\u548c\u66f4\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C# \u4e2d\uff0c\u7d22\u5f15\u5668\uff08Indexer\uff09\u4e0d\u4ec5\u53ef\u4ee5\u7b80\u5355\u5730\u7528\u6765\u5c01\u88c5\u6570\u7ec4\u6216\u5217\u8868\u7684\u8bbf\u95ee\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff0c\u5982 re [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3574","post","type-post","status-publish","format-standard","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3574"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3574\/revisions"}],"predecessor-version":[{"id":3575,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3574\/revisions\/3575"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}