{"id":353,"date":"2021-03-19T08:30:57","date_gmt":"2021-03-19T08:30:57","guid":{"rendered":"http:\/\/robinluo.top\/?p=353"},"modified":"2021-03-19T08:30:57","modified_gmt":"2021-03-19T08:30:57","slug":"c-%e6%b3%9b%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=353","title":{"rendered":"C# \u6cdb\u578b"},"content":{"rendered":"\n<p>\u6cdb\u578b\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\r\nusing System.Collections.Generic;\r\n\r\nnamespace GenericApplication\r\n{\r\n    public class MyGenericArray&lt;T>\r\n    {\r\n        private T&#91;] array;\r\n        public MyGenericArray(int size)\r\n        {\r\n            array = new T&#91;size + 1];\r\n        }\r\n        public T getItem(int index)\r\n        {\r\n            return array&#91;index];\r\n        }\r\n        public void setItem(int index, T value)\r\n        {\r\n            array&#91;index] = value;\r\n        }\r\n    }\r\n           \r\n    class Tester\r\n    {\r\n        static void Main(string&#91;] args)\r\n        {\r\n            \/\/ \u58f0\u660e\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\r\n            MyGenericArray&lt;int> intArray = new MyGenericArray&lt;int>(5);\r\n            \/\/ \u8bbe\u7f6e\u503c\r\n            for (int c = 0; c &lt; 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 &lt; 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&lt;char> charArray = new MyGenericArray&lt;char>(5);\r\n            \/\/ \u8bbe\u7f6e\u503c\r\n            for (int c = 0; c &lt; 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 &lt; 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}<\/code><\/pre>\n\n\n\n<p>\u6cdb\u578b\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>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&lt;T>(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&#91;] 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&lt;int>(ref a, ref b);\r\n            Swap&lt;char>(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}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6cdb\u578b\u7c7b \u6cdb\u578b\u65b9\u6cd5<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[50],"tags":[56],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/353"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353"}],"version-history":[{"count":2,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}