{"id":40879,"date":"2023-12-11T14:51:56","date_gmt":"2023-12-11T06:51:56","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=40879"},"modified":"2023-12-11T14:51:56","modified_gmt":"2023-12-11T06:51:56","slug":"c%e4%b8%ad%e7%b4%a2%e5%bc%95%e5%99%a8%e7%9a%84%e5%ae%9e%e7%8e%b0%e8%bf%87%e7%a8%8b%ef%bc%8c%e6%98%af%e5%90%a6%e5%8f%aa%e8%83%bd%e6%a0%b9%e6%8d%ae%e6%95%b0%e5%ad%97%e8%bf%9b%e8%a1%8c%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/c%e4%b8%ad%e7%b4%a2%e5%bc%95%e5%99%a8%e7%9a%84%e5%ae%9e%e7%8e%b0%e8%bf%87%e7%a8%8b%ef%bc%8c%e6%98%af%e5%90%a6%e5%8f%aa%e8%83%bd%e6%a0%b9%e6%8d%ae%e6%95%b0%e5%ad%97%e8%bf%9b%e8%a1%8c%e7%b4%a2%e5%bc%95\/","title":{"rendered":"C#\u4e2d\u7d22\u5f15\u5668\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u662f\u5426\u53ea\u80fd\u6839\u636e\u6570\u5b57\u8fdb\u884c\u7d22\u5f15\uff0c\u8bf7\u63cf\u8ff0\u4e00\u4e0b"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>\u7b54\uff1a<br \/>\n C#\u901a\u8fc7\u63d0\u4f9b\u7d22\u5f15\u5668\uff0c\u53ef\u4ee5\u8c61\u5904\u7406\u6570\u7ec4\u4e00\u6837\u5904\u7406\u5bf9\u8c61\u3002\u7279\u522b\u662f\u5c5e\u6027\uff0c\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u4ee5\u4e00\u4e2aget\u6216set\u65b9\u6cd5\u66b4\u9732\u3002\u7d22\u5f15\u5668\u4e0d\u5355\u80fd\u7d22\u5f15\u6570\u5b57\uff08\u6570\u7ec4\u4e0b\u6807\uff09\uff0c\u8fd8\u80fd\u7d22\u5f15\u4e00\u4e9bHASHMAP\u7684\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\uff0c\u901a\u5e38\u6765\u8bf4\uff0cC#\u4e2d\u7c7b\u7684\u7d22\u5f15\u5668\u901a\u5e38\u53ea\u6709\u4e00\u4e2a\uff0c\u5c31\u662fTHIS\uff0c\u4f46\u4e5f\u53ef\u4ee5\u6709\u65e0\u6570\u4e2a\uff0c\u53ea\u8981\u4f60\u7684\u53c2\u6570\u5217\u8868\u4e0d\u540c\u5c31\u53ef\u4ee5\u4e86\u7d22\u5f15\u5668\u548c\u8fd4\u56de\u503c\u65e0\u5173, \u7d22\u5f15\u5668\u6700\u5927\u7684\u597d\u5904\u662f\u4f7f\u4ee3\u7801\u770b\u4e0a\u53bb\u66f4\u81ea\u7136\uff0c\u66f4\u7b26\u5408\u5b9e\u9645\u7684\u601d\u8003\u6a21\u5f0f\u3002<\/p>\n<p>&lt;strong&gt;\u5fae\u8f6f\u5b98\u65b9\u4e00\u4e2a\u793a\u4f8b\uff1a&lt;\/strong&gt;<\/p>\n<p>\u7d22\u5f15\u5668\u5141\u8bb8\u7c7b\u6216\u7ed3\u6784\u7684\u5b9e\u4f8b\u6309\u7167\u4e0e\u6570\u7ec4\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u7d22\u5f15\u3002<br \/>\n \u7d22\u5f15\u5668\u7c7b\u4f3c\u4e8e\u5c5e\u6027\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5b83\u4eec\u7684\u8bbf\u95ee\u5668\u91c7\u7528\u53c2\u6570\u3002<br \/>\n \u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6cdb\u578b\u7c7b\uff08class SampleCollection&lt;t&gt;<br \/>\n \uff09\uff0c\u5e76\u4e3a\u5176\u63d0\u4f9b\u4e86\u7b80\u5355\u7684 get \u548c set\u8bbf\u95ee\u5668 \u65b9\u6cd5\uff08\u4f5c\u4e3a\u5206\u914d\u548c\u68c0\u7d22\u503c\u7684\u65b9\u6cd5\uff09\u3002Program \u7c7b\u4e3a\u5b58\u50a8\u5b57\u7b26\u4e32\u521b\u5efa\u4e86\u6b64\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002&lt;\/t&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-cpp&quot;&quot; lang=&quot;&quot;cpp&quot;&quot;&gt;class SampleCollection&lt;T&gt;<br \/>\n{<br \/>\n    private T[] arr = new T[100];<br \/>\n    public T this[int i]   \/\/\u6ce8\u610f\uff0c\u5b9a\u4e49\u7d22\u5f15\u5668\u3002this \u5173\u952e\u5b57\u7528\u4e8e\u5b9a\u4e49\u7d22\u5f15\u5668\u3002<br \/>\n    {<br \/>\n        get<br \/>\n        {<br \/>\n            return arr[i]; \/\/\u8bbf\u95ee\u5668\u91c7\u7528\u53c2\u6570<br \/>\n        }<br \/>\n        set<br \/>\n        {<br \/>\n            arr[i] = value; \/\/\u8bbf\u95ee\u5668\u91c7\u7528\u53c2\u6570<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\nclass Employee<br \/>\n    {<br \/>\n        public string firstName;<br \/>\n        public string middleName;<br \/>\n        public string lastName;<\/p>\n<p>        public string this[string index]<br \/>\n        {<br \/>\n            set<br \/>\n            {<br \/>\n                switch (index)<br \/>\n                {<br \/>\n                    case &quot;&quot;a&quot;&quot;:<br \/>\n                        firstName = value;<br \/>\n                        break;<br \/>\n                    case &quot;&quot;b&quot;&quot;:<br \/>\n                        middleName = value;<br \/>\n                        break;<br \/>\n                    case &quot;&quot;c&quot;&quot;:<br \/>\n                        lastName = value;<br \/>\n                        break;<br \/>\n                    default: throw new ArgumentOutOfRangeException(&quot;&quot;index&quot;&quot;);<br \/>\n                }<br \/>\n            }<br \/>\n            get<br \/>\n            {<br \/>\n                switch (index)<br \/>\n                {<br \/>\n                    case &quot;&quot;a&quot;&quot;: return firstName;<br \/>\n                    case &quot;&quot;b&quot;&quot;: return middleName;<br \/>\n                    case &quot;&quot;c&quot;&quot;: return lastName;<br \/>\n                    default: throw new ArgumentOutOfRangeException(&quot;&quot;index&quot;&quot;);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n\/\/ This class shows how client code uses the indexer<br \/>\nclass Program<br \/>\n{<br \/>\n    static void Main(string[] args)<br \/>\n    {<br \/>\n        SampleCollection&lt;string&gt; stringCollection = new SampleCollection&lt;string&gt;();<br \/>\n        stringCollection[0] = &quot;&quot;Hello, World&quot;&quot;; \/\/\u8fd9\u91cc \u4f7f\u7528\u7d22\u5f15\u5668\u8fdb\u884c\u5f15\u7528<br \/>\n        Console.WriteLine(stringCollection[0]);<br \/>\n        Employee ee = new Employee();<br \/>\n        ee.firstName = &quot;&quot;\u80e1&quot;&quot;;<br \/>\n        ee.middleName = &quot;&quot;\u5927&quot;&quot;;<br \/>\n        ee.lastName = &quot;&quot;\u9633&quot;&quot;;<br \/>\n        ee[&quot;&quot;a&quot;&quot;] = &quot;&quot;sa&quot;&quot;;<br \/>\n        Console.WriteLine(&quot;&quot;\u6211\u7684\u540d\u5b57\u53eb: {0}{1}{2}&quot;&quot;, ee[&quot;&quot;a&quot;&quot;], ee[&quot;&quot;b&quot;&quot;], ee[&quot;&quot;c&quot;&quot;]);<\/p>\n<p>    }<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;ul&gt;<br \/>\n&lt;li&gt;\u7d22\u5f15\u5668\u4f7f\u5f97\u5bf9\u8c61\u53ef\u6309\u7167\u4e0e\u6570\u7ec4\u76f8\u4f3c\u7684\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u3002&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;strong&gt;get&lt;\/strong&gt; \u8bbf\u95ee\u5668\u8fd4\u56de\u503c\u3002&lt;strong&gt;set&lt;\/strong&gt; \u8bbf\u95ee\u5668\u5206\u914d\u503c\u3002&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;strong&gt;this&lt;\/strong&gt;\u5173\u952e\u5b57\u7528\u4e8e\u5b9a\u4e49\u7d22\u5f15\u5668\u3002&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;strong&gt;value&lt;\/strong&gt;\u5173\u952e\u5b57\u7528\u4e8e\u5b9a\u4e49\u7531 &lt;strong&gt;set&lt;\/strong&gt; \u7d22\u5f15\u5668\u5206\u914d\u7684\u503c\u3002&lt;\/li&gt;<br \/>\n&lt;li&gt;\u7d22\u5f15\u5668\u4e0d\u5fc5\u6839\u636e\u6574\u6570\u503c\u8fdb\u884c\u7d22\u5f15\uff0c\u7531\u60a8\u51b3\u5b9a\u5982\u4f55\u5b9a\u4e49\u7279\u5b9a\u7684\u67e5\u627e\u673a\u5236\u3002&lt;\/li&gt;<br \/>\n&lt;li&gt;\u7d22\u5f15\u5668\u53ef\u88ab\u91cd\u8f7d\u3002&lt;\/li&gt;<br \/>\n&lt;li&gt;\u7d22\u5f15\u5668\u53ef\u4ee5\u6709\u591a\u4e2a\u5f62\u53c2\uff0c\u4f8b\u5982\u5f53\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u65f6\u3002&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u7b54\uff1a C#\u901a\u8fc7\u63d0\u4f9b\u7d22\u5f15\u5668\uff0c\u53ef\u4ee5\u8c61\u5904\u7406\u6570\u7ec4\u4e00\u6837\u5904\u7406\u5bf9\u8c61\u3002\u7279\u522b\u662f\u5c5e\u6027\uff0c\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-40879","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/40879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=40879"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/40879\/revisions"}],"predecessor-version":[{"id":40880,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/40879\/revisions\/40880"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=40879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=40879"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=40879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}