\u7f16\u5199\u533f\u540d\u65b9\u6cd5\u7684\u8bed\u6cd5<\/strong><\/div>\n\u533f\u540d\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528 delegate \u5173\u952e\u5b57\u521b\u5efa\u59d4\u6258\u5b9e\u4f8b\u6765\u58f0\u660e\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n
delegate void NumberChanger(int n);\r\n...\r\nNumberChanger nc = delegate(int x)\r\n{\r\n Console.WriteLine(\"Anonymous Method: {0}\", x);\r\n};<\/pre>\n\u4ee3\u7801\u5757 Console.WriteLine(\"Anonymous Method: {0}\", x); \u662f\u533f\u540d\u65b9\u6cd5\u7684\u4e3b\u4f53\u3002<\/p>\n
\u59d4\u6258\u53ef\u4ee5\u901a\u8fc7\u533f\u540d\u65b9\u6cd5\u8c03\u7528\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u547d\u540d\u65b9\u6cd5\u8c03\u7528\uff0c\u5373\uff0c\u901a\u8fc7\u5411\u59d4\u6258\u5bf9\u8c61\u4f20\u9012\u65b9\u6cd5\u53c2\u6570\u3002<\/p>\n
\u6ce8\u610f: \u533f\u540d\u65b9\u6cd5\u7684\u4e3b\u4f53\u540e\u9762\u9700\u8981\u4e00\u4e2a ;\u3002<\/p>\n
\u4f8b\u5982\uff1a<\/p>\n
nc(10);<\/pre>\n\u5b9e\u4f8b<\/strong><\/div>\n\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u533f\u540d\u65b9\u6cd5\u7684\u6982\u5ff5\uff1a<\/p>\n
\u5b9e\u4f8b<\/p>\n
using System;\r\n\r\ndelegate void NumberChanger(int n);\r\nnamespace DelegateAppl\r\n{\r\n class TestDelegate\r\n {\r\n static int num = 10;\r\n public static void AddNum(int p)\r\n {\r\n num += p;\r\n Console.WriteLine(\"Named Method: {0}\", num);\r\n }\r\n\r\n public static void MultNum(int q)\r\n {\r\n num *= q;\r\n Console.WriteLine(\"Named Method: {0}\", num);\r\n }\r\n\r\n static void Main(string[] args)\r\n {\r\n \/\/ \u4f7f\u7528\u533f\u540d\u65b9\u6cd5\u521b\u5efa\u59d4\u6258\u5b9e\u4f8b\r\n NumberChanger nc = delegate(int x)\r\n {\r\n Console.WriteLine(\"Anonymous Method: {0}\", x);\r\n };\r\n \r\n \/\/ \u4f7f\u7528\u533f\u540d\u65b9\u6cd5\u8c03\u7528\u59d4\u6258\r\n nc(10);\r\n\r\n \/\/ \u4f7f\u7528\u547d\u540d\u65b9\u6cd5\u5b9e\u4f8b\u5316\u59d4\u6258\r\n nc = new NumberChanger(AddNum);\r\n \r\n \/\/ \u4f7f\u7528\u547d\u540d\u65b9\u6cd5\u8c03\u7528\u59d4\u6258\r\n nc(5);\r\n\r\n \/\/ \u4f7f\u7528\u53e6\u4e00\u4e2a\u547d\u540d\u65b9\u6cd5\u5b9e\u4f8b\u5316\u59d4\u6258\r\n nc = new NumberChanger(MultNum);\r\n \r\n \/\/ \u4f7f\u7528\u547d\u540d\u65b9\u6cd5\u8c03\u7528\u59d4\u6258\r\n nc(2);\r\n Console.ReadKey();\r\n }\r\n }\r\n}<\/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
Anonymous Method: 10\r\nNamed Method: 15\r\nNamed Method: 30<\/pre>\n","protected":false},"excerpt":{"rendered":"\u533f\u540d\u65b9\u6cd5\uff08Anonymous methods\uff09 \u63d0\u4f9b\u4e86\u4e00\u79cd\u4f20\u9012\u4ee3\u7801\u5757\u4f5c\u4e3a\u59d4\u6258\u53c2\u6570\u7684\u6280\u672f\u3002\u533f\u540d\u65b9\u6cd5\u662f\u6ca1\u6709\u540d\u79f0\u53ea\u6709 […]<\/p>\n","protected":false},"author":321,"featured_media":248358,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-248356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=248356"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248356\/revisions"}],"predecessor-version":[{"id":248361,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248356\/revisions\/248361"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/248358"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=248356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=248356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=248356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}