{"id":3571,"date":"2025-04-19T10:20:11","date_gmt":"2025-04-19T02:20:11","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3571"},"modified":"2025-04-19T10:41:25","modified_gmt":"2025-04-19T02:41:25","slug":"c-%e7%b4%a2%e5%bc%95%e5%99%a8%ef%bc%88indexer%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/c-%e7%b4%a2%e5%bc%95%e5%99%a8%ef%bc%88indexer%ef%bc%89\/","title":{"rendered":"C# \u7d22\u5f15\u5668\uff08Indexer\uff09"},"content":{"rendered":"\n<p>C# <strong>\u7d22\u5f15\u5668\uff08Indexer\uff09<\/strong> \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5c5e\u6027\uff08Property\uff09\uff0c\u4f7f\u4f60\u53ef\u4ee5\u50cf\u8bbf\u95ee\u6570\u7ec4\u4e00\u6837\u901a\u8fc7 <strong><code>[]<\/code><\/strong> \u64cd\u4f5c\u7b26\u8bbf\u95ee\u5bf9\u8c61\u7684\u5185\u90e8\u6570\u636e\u3002\u8fd9\u5728\u81ea\u5b9a\u4e49\u96c6\u5408\u7c7b\u3001\u5c01\u88c5\u591a\u7ef4\u6570\u636e\u3001\u8868\u8fbe\u8bed\u4e49\u5316\u8bbf\u95ee\u65b9\u5f0f\u65f6\u975e\u5e38\u6709\u7528\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. \u7d22\u5f15\u5668\u8bed\u6cd5\u4e0e\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SampleCollection&lt;T&gt;\n{\n    private T&#91;] arr = new T&#91;100];\n\n    public T this&#91;int i]\n    {\n        get =&gt; arr&#91;i];\n        set =&gt; arr&#91;i] = value;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u8bf4\u660e\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>this[int i]<\/code>\uff1a\u7d22\u5f15\u5668\u58f0\u660e\uff0c<code>int<\/code> \u662f\u7d22\u5f15\u7c7b\u578b\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff08\u4f8b\u5982 <code>string<\/code>, <code>enum<\/code>, \u591a\u4e2a\u53c2\u6570\u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>get\/set<\/code>\uff1a\u8bbf\u95ee\u6216\u4fee\u6539\u7d22\u5f15\u5bf9\u5e94\u7684\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\">2. \u5b9e\u6218\u793a\u4f8b\uff1a\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u5b57\u5178\u8bbf\u95ee<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class StringDictionary\n{\n    private Dictionary&lt;string, string&gt; data = new();\n\n    public string this&#91;string key]\n    {\n        get =&gt; data.ContainsKey(key) ? data&#91;key] : null;\n        set =&gt; data&#91;key] = value;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u7528\u6cd5\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var dict = new StringDictionary();\ndict&#91;\"Name\"] = \"ChatGPT\";\nConsole.WriteLine(dict&#91;\"Name\"]); \/\/ \u8f93\u51fa\uff1aChatGPT<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u591a\u53c2\u6570\u7d22\u5f15\u5668<\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u7d22\u5f15\u53c2\u6570\uff0c\u7c7b\u4f3c\u4e8c\u7ef4\u6570\u7ec4\u7684\u8bbf\u95ee\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Matrix\n{\n    private int&#91;,] data = new int&#91;10, 10];\n\n    public int this&#91;int x, int y]\n    {\n        get =&gt; data&#91;x, y];\n        set =&gt; data&#91;x, y] = value;\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 m = new Matrix();\nm&#91;1, 2] = 42;\nConsole.WriteLine(m&#91;1, 2]); \/\/ \u8f93\u51fa 42<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u4f7f\u7528\u7d22\u5f15\u5668\u589e\u5f3a\u96c6\u5408\u7c7b\u5c01\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class BookShelf\n{\n    private List&lt;string&gt; books = new();\n\n    public string this&#91;int index]\n    {\n        get =&gt; books&#91;index];\n        set =&gt; books&#91;index] = value;\n    }\n\n    public void Add(string book) =&gt; books.Add(book);\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 shelf = new BookShelf();\nshelf.Add(\"C# Basics\");\nshelf.Add(\"Advanced C#\");\n\nConsole.WriteLine(shelf&#91;1]); \/\/ \u8f93\u51fa\uff1aAdvanced C#<\/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. \u4e0e\u5c5e\u6027\u7684\u533a\u522b\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u5c5e\u6027\uff08Property\uff09<\/th><th>\u7d22\u5f15\u5668\uff08Indexer\uff09<\/th><\/tr><\/thead><tbody><tr><td>\u8bed\u6cd5<\/td><td>\u901a\u8fc7\u540d\u79f0\u8bbf\u95ee<\/td><td>\u901a\u8fc7 <code>[]<\/code> \u8bbf\u95ee<\/td><\/tr><tr><td>\u540d\u79f0<\/td><td>\u6709\u540d\u79f0\uff08\u5982 <code>Name<\/code>\uff09<\/td><td>\u6ca1\u6709\u540d\u79f0\uff0c\u59cb\u7ec8\u662f <code>this<\/code><\/td><\/tr><tr><td>\u5e94\u7528\u573a\u666f<\/td><td>\u63a7\u5236\u5355\u4e2a\u503c\u7684\u8bbf\u95ee\u903b\u8f91<\/td><td>\u63a7\u5236\u96c6\u5408\u4e2d\u591a\u4e2a\u503c\u7684\u8bbf\u95ee\u903b\u8f91<\/td><\/tr><tr><td>\u53ef\u7d22\u5f15\u7c7b\u578b<\/td><td>\u274c \u4e00\u822c\u4e0d\u53ef\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee<\/td><td>\u2705 \u652f\u6301 <code>int<\/code>, <code>string<\/code> \u7b49\u7d22\u5f15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u63a5\u53e3 + \u7d22\u5f15\u5668\uff08\u5178\u578b\u5e94\u7528\uff09<\/h2>\n\n\n\n<p>.NET \u4e2d <code>IList&lt;T&gt;<\/code> \u548c <code>IDictionary&lt;TKey, TValue&gt;<\/code> \u5c31\u7528\u5230\u4e86\u7d22\u5f15\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface IList&lt;T&gt; : ICollection&lt;T&gt;\n{\n    T this&#91;int index] { get; set; }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface IDictionary&lt;TKey, TValue&gt; : ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;\n{\n    TValue this&#91;TKey key] { get; set; }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u6ce8\u610f\u4e8b\u9879\u4e0e\u9650\u5236<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7d22\u5f15\u5668<strong>\u4e0d\u80fd\u4e3a\u9759\u6001<\/strong>\u3002<\/li>\n\n\n\n<li>\u652f\u6301<strong>\u91cd\u8f7d<\/strong>\uff08\u901a\u8fc7\u4e0d\u540c\u53c2\u6570\u6570\u91cf\u6216\u7c7b\u578b\uff09\u3002<\/li>\n\n\n\n<li>\u7d22\u5f15\u5668\u672c\u8d28\u4e0a\u662f\u8bed\u6cd5\u7cd6\uff0c\u4ecd\u7136\u901a\u8fc7 <code>get<\/code> \u548c <code>set<\/code> \u65b9\u6cd5\u6267\u884c\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>Microsoft \u5b98\u65b9\u6587\u6863\uff1a<br>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/indexers\/\">C# Indexers &#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>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# \u7d22\u5f15\u5668\uff08Indexer\uff09 \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5c5e\u6027\uff08Property\uff09\uff0c\u4f7f\u4f60\u53ef\u4ee5\u50cf\u8bbf\u95ee\u6570\u7ec4\u4e00\u6837\u901a\u8fc7 [] \u64cd\u4f5c\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3571","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=3571"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3571\/revisions"}],"predecessor-version":[{"id":3573,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3571\/revisions\/3573"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3572"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}