{"id":287,"date":"2022-06-29T16:07:25","date_gmt":"2022-06-29T08:07:25","guid":{"rendered":"https:\/\/www.fengjijiao.cn\/?p=287"},"modified":"2022-06-29T16:07:26","modified_gmt":"2022-06-29T08:07:26","slug":"c%e7%b4%a2%e5%bc%95%e5%99%a8%ef%bc%88indexer%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.fengjijiao.cn\/?p=287","title":{"rendered":"C#\u7d22\u5f15\u5668\uff08Indexer\uff09"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System;\r\n\r\nnamespace ConsoleApp1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string&#91;] args)\r\n        {\r\n            \/\/C#\u7d22\u5f15\u5668\uff08Indexer\uff09\r\n            \/*\r\n             * \u7d22\u5f15\u5668\uff08Indexer\uff09\u5141\u8bb8\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u50cf\u6570\u7ec4\u4e00\u6837\u4f7f\u7528\u4e0b\u6807\u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u3002\r\n             * \u5f53\u4f60\u4e3a\u7c7b\u5b9a\u4e49\u4e00\u4e2a\u7d22\u5f15\u5668\u65f6\uff0c\u8be5\u7c7b\u7684\u884c\u4e3a\u5c31\u4f1a\u50cf\u4e00\u4e2a\u865a\u62df\u6570\u7ec4\uff08virtual array\uff09\u4e00\u6837\u3002\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u8bbf\u95ee\u8fd0\u7b97\u7b26&#91;]\u6765\u8bbf\u95ee\u8be5\u7c7b\u7684\u6210\u5458\u3002\r\n             * \u8bed\u6cd5\r\n             * \u4e00\u7ef4\u7d22\u5f15\u5668\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a\r\n             * element-type thi&#91;int index]\r\n             * {\r\n             *     \/\/get\u8bbf\u95ee\u5668\r\n             *     get\r\n             *     {\r\n             *     \/\/\u8fd4\u56deindex\u6307\u5b9a\u7684\u503c\r\n             *     }\r\n             *     \/\/set\u8bbf\u95ee\u5668\r\n             *     set\r\n             *     {\r\n             *     \/\/\u8bbe\u7f6eindex\u6307\u5b9a\u7684\u503c\r\n             *     }\r\n             *     \r\n             * }\r\n             * \r\n             * \u7528\u9014\r\n             * \u7d22\u5f15\u5668\u7684\u884c\u4e3a\u58f0\u660e\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u7c7b\u4f3c\u4e8e\u5c5e\u6027\uff08property\uff09\u3002\u5c31\u50cf\u5c5e\u6027(property)\uff0c\u53ef\u4ee5\u4f7f\u7528get\u548cset\u8bbf\u95ee\u5668\u6765\u5b9a\u4e49\u7d22\u5f15\u5668\u3002\u4f46\u662f\uff0c\u5c5e\u6027\u8fd4\u56de\u6216\u8bbe\u7f6e\u4e00\u4e2a\u7279\u5b9a\u7684\u6570\u636e\u6210\u5458\uff0c\u800c\u7d22\u5f15\u5668\u8fd4\u56de\u6216\u8bbe\u7f6e\u5bf9\u8c61\u5b9e\u4f8b\u7684\u4e00\u4e2a\u7279\u5b9a\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u628a\u5b9e\u4f8b\u6570\u636e\u5206\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u5e76\u7d22\u5f15\u6bcf\u4e2a\u90e8\u5206\uff0c\u83b7\u53d6\u6216\u8bbe\u7f6e\u6bcf\u4e2a\u90e8\u5206\u3002\r\n             * \u5b9a\u4e49\u4e00\u4e2a\u5c5e\u6027(property)\u5305\u542b\u63d0\u4f9b\u5c5e\u6027\u540d\u79f0\u3002\u7d22\u5f15\u5668\u5b9a\u4e49\u7684\u65f6\u5019\u4e0d\u5e26\u6709\u540d\u79f0\uff0c\u4f46\u5e26\u6709this\u5173\u952e\u5b57\uff0c\u5b83\u6307\u5411\u5bf9\u8c61\u5b9e\u4f8b\u3002\r\n             * \r\n             *\/\r\n            IndexedNames names = new IndexedNames();\r\n            names&#91;0] = \"Zara\";\r\n            names&#91;1] = \"Riz\";\r\n            names&#91;2] = \"Nuha\";\r\n            names&#91;3] = \"Asif\";\r\n            names&#91;4] = \"Davinder\";\r\n            names&#91;5] = \"Sunil\";\r\n            names&#91;6] = \"Rubic\";\r\n            for(int i = 0;i &lt; IndexedNames.size; i++)\r\n            {\r\n                Console.WriteLine(names&#91;i]);\r\n            }\r\n            \/\/\u91cd\u8f7d\u7d22\u5f15\u5668\uff08Indexer\uff09\r\n            \/*\r\n             * \u7d22\u5f15\u5668(indexer)\u53ef\u88ab\u91cd\u8f7d\u3002\u7d22\u5f15\u5668\u58f0\u660e\u7684\u65f6\u5019\u4e5f\u53ef\u5e26\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u4e14\u6bcf\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u6ca1\u6709\u5fc5\u8981\u8ba9\u7d22\u5f15\u5668\u5fc5\u987b\u662f\u6574\u578b\u7684\u3002C#\u5141\u8bb8\u7d22\u5f15\u5668\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\r\n             * \r\n             *\/\r\n            IndexedNames2 names2 = new IndexedNames2();\r\n            names2&#91;0] = \"Zara\";\r\n            names2&#91;1] = \"Riz\";\r\n            names2&#91;2] = \"Nuha\";\r\n            names2&#91;3] = \"Asif\";\r\n            names2&#91;4] = \"Davinder\";\r\n            names2&#91;5] = \"Sunil\";\r\n            names2&#91;6] = \"Rubic\";\r\n            for (int i = 0; i &lt; IndexedNames2.size; i++)\r\n            {\r\n                Console.WriteLine(names&#91;i]);\r\n            }\r\n            \/\/\u4f7f\u7528\u91cd\u8f7d\u7d22\u5f15\u5668\u67e5\u8be2\u7279\u5b9a\u503c\u7684\u7d22\u5f15\r\n            Console.WriteLine(\"Nuha: {0}\", names2&#91;\"Nuha\"]);\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n\r\n    class IndexedNames\r\n    {\r\n        private string&#91;] namelist = new string&#91;size];\r\n        static public int size = 10;\r\n        public IndexedNames()\r\n        {\r\n            for (int i = 0; i &lt; size; i++)\r\n                namelist&#91;i] = \"N.A\";\r\n        }\r\n        public string this&#91;int index]\r\n        {\r\n            get\r\n            {\r\n                string tmp;\r\n                if(index >= 0 &amp;&amp; index &lt;= size - 1)\r\n                {\r\n                    tmp = namelist&#91;index];\r\n                }\r\n                else\r\n                {\r\n                    tmp = \"\";\r\n                }\r\n                return (tmp);\r\n            }\r\n            set\r\n            {\r\n                if(index >= 0 &amp;&amp; index &lt;= size - 1)\r\n                {\r\n                    namelist&#91;index] = value;\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    class IndexedNames2\r\n    {\r\n        private string&#91;] namelist = new string&#91;size];\r\n        static public int size = 10;\r\n        public IndexedNames2()\r\n        {\r\n            for (int i = 0; i &lt; size; i++)\r\n                namelist&#91;i] = \"N.A\";\r\n        }\r\n        public string this&#91;int index]\r\n        {\r\n            get\r\n            {\r\n                string tmp;\r\n                if (index >= 0 &amp;&amp; index &lt;= size - 1)\r\n                {\r\n                    tmp = namelist&#91;index];\r\n                }\r\n                else\r\n                {\r\n                    tmp = \"\";\r\n                }\r\n                return (tmp);\r\n            }\r\n            set\r\n            {\r\n                if (index >= 0 &amp;&amp; index &lt;= size - 1)\r\n                {\r\n                    namelist&#91;index] = value;\r\n                }\r\n            }\r\n        }\r\n        public int this&#91;string name]\r\n        {\r\n            get\r\n            {\r\n                int index = 0;\r\n                while(index &lt; size)\r\n                {\r\n                    if(namelist&#91;index] == name)\r\n                    {\r\n                        return index;\r\n                    }\r\n                    index++;\r\n                }\r\n                return index;\r\n            }\r\n        }\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/287"}],"collection":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":1,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":288,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/288"}],"wp:attachment":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}