{"id":248350,"date":"2022-07-30T09:20:14","date_gmt":"2022-07-30T01:20:14","guid":{"rendered":"https:\/\/gulass.cn\/?p=248350"},"modified":"2022-07-22T11:22:43","modified_gmt":"2022-07-22T03:22:43","slug":"about-c-generic","status":"publish","type":"post","link":"https:\/\/gulass.cn\/about-c-generic.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u6cdb\u578b\uff08Generic\uff09"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6cdb\u578b\uff08Generic\uff09 \u5141\u8bb8\u60a8\u5ef6\u8fdf\u7f16\u5199\u7c7b\u6216\u65b9\u6cd5\u4e2d\u7684\u7f16\u7a0b\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u7684\u89c4\u8303\uff0c\u76f4\u5230\u5b9e\u9645\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5b83\u7684\u65f6\u5019\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6cdb\u578b\u5141\u8bb8\u60a8\u7f16\u5199\u4e00\u4e2a\u53ef\u4ee5\u4e0e\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u4e00\u8d77\u5de5\u4f5c\u7684\u7c7b\u6216\u65b9\u6cd5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u60a8\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u7c7b\u578b\u7684\u66ff\u4ee3\u53c2\u6570\u7f16\u5199\u7c7b\u6216\u65b9\u6cd5\u7684\u89c4\u8303\u3002\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u751f\u6210\u4ee3\u7801\u6765\u5904\u7406\u6307\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\u5c06\u6709\u52a9\u4e8e\u60a8\u7406\u89e3\u8fd9\u4e2a\u6982\u5ff5\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nusing System.Collections.Generic;\r\n\r\nnamespace GenericApplication\r\n{\r\n    public class MyGenericArray\r\n    {\r\n        private T[] array;\r\n        public MyGenericArray(int size)\r\n        {\r\n            array = new T[size + 1];\r\n        }\r\n        public T getItem(int index)\r\n        {\r\n            return array[index];\r\n        }\r\n        public void setItem(int index, T value)\r\n        {\r\n            array[index] = value;\r\n        }\r\n    }\r\n           \r\n    class Tester\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            \/\/ \u58f0\u660e\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\r\n            MyGenericArray intArray = new MyGenericArray<\/int>(5);\r\n            \/\/ \u8bbe\u7f6e\u503c\r\n            for (int c = 0; c < 5; c++)\r\n            {\r\n                intArray.setItem(c, c*5);\r\n            }\r\n            \/\/ \u83b7\u53d6\u503c\r\n            for (int c = 0; c < 5; c++)\r\n            {\r\n                Console.Write(intArray.getItem(c) + \" \");\r\n            }\r\n            Console.WriteLine();\r\n            \/\/ \u58f0\u660e\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\r\n            MyGenericArray charArray = new MyGenericArray<\/char>(5);\r\n            \/\/ \u8bbe\u7f6e\u503c\r\n            for (int c = 0; c < 5; c++)\r\n            {\r\n                charArray.setItem(c, (char)(c+97));\r\n            }\r\n            \/\/ \u83b7\u53d6\u503c\r\n            for (int c = 0; c < 5; c++)\r\n            {\r\n                Console.Write(charArray.getItem(c) + \" \");\r\n            }\r\n            Console.WriteLine();\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n}<\/char><\/int><\/t><\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

0 5 10 15 20\r\na b c d e<\/pre>\n
\u6cdb\u578b\uff08Generic\uff09\u7684\u7279\u6027<\/strong><\/div>\n

\u4f7f\u7528\u6cdb\u578b\u662f\u4e00\u79cd\u589e\u5f3a\u7a0b\u5e8f\u529f\u80fd\u7684\u6280\u672f\uff0c\u5177\u4f53\u8868\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n

    \n
  1. \u5b83\u6709\u52a9\u4e8e\u60a8\u6700\u5927\u9650\u5ea6\u5730\u91cd\u7528\u4ee3\u7801\u3001\u4fdd\u62a4\u7c7b\u578b\u7684\u5b89\u5168\u4ee5\u53ca\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n
  2. \u60a8\u53ef\u4ee5\u521b\u5efa\u6cdb\u578b\u96c6\u5408\u7c7b\u3002.NET \u6846\u67b6\u7c7b\u5e93\u5728 System.Collections.Generic \u547d\u540d\u7a7a\u95f4\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u65b0\u7684\u6cdb\u578b\u96c6\u5408\u7c7b\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6cdb\u578b\u96c6\u5408\u7c7b\u6765\u66ff\u4ee3 System.Collections \u4e2d\u7684\u96c6\u5408\u7c7b\u3002<\/li>\n
  3. \u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u6cdb\u578b\u63a5\u53e3\u3001\u6cdb\u578b\u7c7b\u3001\u6cdb\u578b\u65b9\u6cd5\u3001\u6cdb\u578b\u4e8b\u4ef6\u548c\u6cdb\u578b\u59d4\u6258\u3002<\/li>\n
  4. \u60a8\u53ef\u4ee5\u5bf9\u6cdb\u578b\u7c7b\u8fdb\u884c\u7ea6\u675f\u4ee5\u8bbf\u95ee\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u3002<\/li>\n
  5. \u5173\u4e8e\u6cdb\u578b\u6570\u636e\u7c7b\u578b\u4e2d\u4f7f\u7528\u7684\u7c7b\u578b\u7684\u4fe1\u606f\u53ef\u5728\u8fd0\u884c\u65f6\u901a\u8fc7\u4f7f\u7528\u53cd\u5c04\u83b7\u53d6\u3002<\/li>\n<\/ol>\n
    \u6cdb\u578b\uff08Generic\uff09\u65b9\u6cd5<\/strong><\/div>\n

    \u5728\u4e0a\u9762\u7684\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u4e86\u6cdb\u578b\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u53c2\u6570\u58f0\u660e\u6cdb\u578b\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e2a\u6982\u5ff5\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    using System;\r\nusing System.Collections.Generic;\r\n\r\nnamespace GenericMethodAppl\r\n{\r\n    class Program\r\n    {\r\n        static void Swap(ref T lhs, ref T rhs)\r\n        {\r\n            T temp;\r\n            temp = lhs;\r\n            lhs = rhs;\r\n            rhs = temp;\r\n        }\r\n        static void Main(string[] args)\r\n        {\r\n            int a, b;\r\n            char c, d;\r\n            a = 10;\r\n            b = 20;\r\n            c = 'I';\r\n            d = 'V';\r\n\r\n            \/\/ \u5728\u4ea4\u6362\u4e4b\u524d\u663e\u793a\u503c\r\n            Console.WriteLine(\"Int values before calling swap:\");\r\n            Console.WriteLine(\"a = {0}, b = {1}\", a, b);\r\n            Console.WriteLine(\"Char values before calling swap:\");\r\n            Console.WriteLine(\"c = {0}, d = {1}\", c, d);\r\n\r\n            \/\/ \u8c03\u7528 swap\r\n            Swap(ref a, ref b);\r\n            Swap(ref c, ref d);\r\n\r\n            \/\/ \u5728\u4ea4\u6362\u4e4b\u540e\u663e\u793a\u503c\r\n            Console.WriteLine(\"Int values after calling swap:\");\r\n            Console.WriteLine(\"a = {0}, b = {1}\", a, b);\r\n            Console.WriteLine(\"Char values after calling swap:\");\r\n            Console.WriteLine(\"c = {0}, d = {1}\", c, d);\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n}<\/char><\/int><\/t><\/pre>\n

    \u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

    Int values before calling swap:\r\na = 10, b = 20\r\nChar values before calling swap:\r\nc = I, d = V\r\nInt values after calling swap:\r\na = 20, b = 10\r\nChar values after calling swap:\r\nc = V, d = I<\/pre>\n
    \u6cdb\u578b\uff08Generic\uff09\u59d4\u6258<\/strong><\/div>\n

    \u60a8\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u53c2\u6570\u5b9a\u4e49\u6cdb\u578b\u59d4\u6258\u3002\u4f8b\u5982\uff1a<\/p>\n

    delegate T NumberChanger(T n);<\/t><\/pre>\n

    \u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u59d4\u6258\u7684\u4f7f\u7528\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    using System;\r\nusing System.Collections.Generic;\r\n\r\ndelegate T NumberChanger(T n);\r\nnamespace GenericDelegateAppl\r\n{\r\n    class TestDelegate\r\n    {\r\n        static int num = 10;\r\n        public static int AddNum(int p)\r\n        {\r\n            num += p;\r\n            return num;\r\n        }\r\n\r\n        public static int MultNum(int q)\r\n        {\r\n            num *= q;\r\n            return num;\r\n        }\r\n        public static int getNum()\r\n        {\r\n            return num;\r\n        }\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            \/\/ \u521b\u5efa\u59d4\u6258\u5b9e\u4f8b\r\n            NumberChanger nc1 = new NumberChanger<\/int>(AddNum);\r\n            NumberChanger<\/int> nc2 = new NumberChanger<\/int>(MultNum);\r\n            \/\/ \u4f7f\u7528\u59d4\u6258\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5\r\n            nc1(25);\r\n            Console.WriteLine(\"Value of Num: {0}\", getNum());\r\n            nc2(5);\r\n            Console.WriteLine(\"Value of Num: {0}\", getNum());\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n}<\/int><\/t><\/pre>\n

    \u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

    Value of Num: 35\r\nValue of Num: 175<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u60a8\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u7c7b\u578b\u7684\u66ff\u4ee3\u53c2\u6570\u7f16\u5199\u7c7b\u6216\u65b9\u6cd5\u7684\u89c4\u8303\u3002\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u751f\u6210\u4ee3\u7801\u6765\u5904\u7406 […]<\/p>\n","protected":false},"author":321,"featured_media":248352,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-248350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=248350"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248350\/revisions"}],"predecessor-version":[{"id":248355,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248350\/revisions\/248355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/248352"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=248350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=248350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=248350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}