{"id":3584,"date":"2020-08-03T07:20:57","date_gmt":"2020-08-02T22:20:57","guid":{"rendered":"https:\/\/okamurax.com\/?p=3584"},"modified":"2020-08-11T22:35:34","modified_gmt":"2020-08-11T13:35:34","slug":"c-%e3%83%87%e3%83%aa%e3%82%b2%e3%83%bc%e3%83%88delegate","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=3584","title":{"rendered":"C# Delegate \u30e1\u30e2"},"content":{"rendered":"<p>\u57fa\u672c<\/p>\n<pre class=\"lang:c# decode:true\">    public partial class Form1 : Form\r\n    {\r\n        \/\/ \u5ba3\u8a00\r\n        \/\/ \u623b\u5024\u306e\u578b \u30c7\u30ea\u30b2\u30fc\u30c8\u540d (\u5f15\u6570)\r\n        delegate int TestDelegate(string s);\r\n\r\n        \/\/ \u30c7\u30ea\u30b2\u30fc\u30c8\u3068\u30e1\u30bd\u30c3\u30c9\u306f\u5f15\u6570\/\u8fd4\u5024\u304c\u540c\u3058\u306b\u3059\u308b\r\n\r\n        private int TestMethod (string msg)\r\n        {\r\n            return msg.Length;\r\n        }\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            \/\/ \u751f\u6210\r\n            \/\/ \u5f15\u6570\u306b\u306f\u30e1\u30bd\u30c3\u30c9\u3092\u6e21\u3059\r\n            TestDelegate td1 = new TestDelegate(TestMethod);\r\n            \/\/ C#2.0\u304b\u3089\u306fnew\u4e0d\u8981(TestDelegate td2 = TestMethod)\u3068\u3082\u66f8\u3051\u308b\r\n\r\n            \/\/ \u5b9f\u884c\r\n            System.Diagnostics.Debug.Print(td1(\"abc\").ToString()); \/\/ 3\r\n        }\r\n    }<\/pre>\n<p>\u8907\u6570\u4ee3\u5165\u3067\u304d\u308b(\u30de\u30eb\u30c1\u30ad\u30e3\u30b9\u30c8)<\/p>\n<pre class=\"lang:c# decode:true \">    public partial class Form1 : Form\r\n    {\r\n        delegate void TestDelegate();\r\n\r\n        private void TestMethod1 ()\r\n        {\r\n            System.Diagnostics.Debug.Print(\"hello\");\r\n        }\r\n        private void TestMethod2 ()\r\n        {\r\n            System.Diagnostics.Debug.Print(\"world\");\r\n        }\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            TestDelegate td = TestMethod1;\r\n            td += TestMethod2;\r\n            td(); \/\/ hello world\r\n\r\n        }\r\n    }<\/pre>\n<p>\u533f\u540d\u95a2\u6570(\u533f\u540d\u30e1\u30bd\u30c3\u30c9\u30fb\u30e9\u30e0\u30c0\u5f0f)<\/p>\n<p>C#2.0\u304b\u3089\u533f\u540d\u30e1\u30bd\u30c3\u30c9\u3001C#3.0\u30e9\u30e0\u30c0\u5f0f<\/p>\n<pre class=\"lang:c# decode:true \">    public partial class Form1 : Form\r\n    {\r\n        delegate int TestDelegate(string x);\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            \/\/ \u533f\u540d\u30e1\u30bd\u30c3\u30c9\r\n            TestDelegate td1 = delegate (string x)\r\n            {\r\n                return x.Length;\r\n            };\r\n\r\n            System.Diagnostics.Debug.Print(td1(\"abc\").ToString()); \/\/ 3\r\n\r\n            \/\/ \u30e9\u30e0\u30c0\u5f0f\r\n            TestDelegate td2 = (x) =&gt;\r\n            {\r\n                return x.Length;\r\n            };\r\n\r\n            System.Diagnostics.Debug.Print(td1(\"hello\").ToString()); \/\/ 5\r\n\r\n        }\r\n    }<\/pre>\n<p>C#2.0\u3067\u30b8\u30a7\u30cd\u30ea\u30c3\u30af(\u578b\u30d1\u30e9\u30fc\u30e1\u30fc\u30bf)\u304c\u5c0e\u5165\u3002Action,Func(\u8fd4\u5024\u6709)\u3092\u4f7f\u3046\u3068Delegate\u306e\u5b9a\u7fa9\u3082\u4e0d\u8981\u306b\u306a\u308b\u3002Action,Func\u7b49\u3092\u5b9a\u7fa9\u6e08\u307f\u30c7\u30ea\u30b2\u30fc\u30c8\u3068\u547c\u3076\u3002<\/p>\n<pre class=\"lang:c# decode:true\">    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            Func&lt;string,int&gt; td = delegate (string x)\r\n            {\r\n                return x.Length;\r\n            };\r\n\r\n            System.Diagnostics.Debug.Print(td(\"abc\").ToString()); \/\/ 3\r\n        }\r\n    }<\/pre>\n<p>\u5b9f\u7528\u6027\u306f\u306a\u3044\u3051\u3069\u3001\u30e9\u30e0\u30c0\u5f0f\u3068\u5b9a\u7fa9\u6e08\u307f\u30c7\u30ea\u30b2\u30fc\u30c8\u3067\u5373\u6642\u5b9f\u884c\u3082\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"lang:c# decode:true \">    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            \/\/ \u30e9\u30e0\u30c0\u5f0f\u3092Func&lt;string&gt;\u306b\u30ad\u30e3\u30b9\u30c8\u3057\u3066\u5373\u6642\u5b9f\u884c\u3057\u3066\u3044\u308b\r\n            MessageBox.Show(\r\n            ((Func&lt;string&gt;)(() =&gt; { return \"hello1\"; }))()\r\n            );\r\n            \r\n            \/\/ \u5f15\u6570\u3092\u3068\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3002\r\n            MessageBox.Show(\r\n            ((Func&lt;string,string&gt;)((string x) =&gt; { return x; }))(\"hello2\")\r\n            );\r\n            \r\n        }\r\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c public partial class Form1 : Form { \/\/ \u5ba3\u8a00 \/\/ \u623b\u5024\u306e\u578b \u30c7\u30ea\u30b2\u30fc\u30c8\u540d (\u5f15\u6570) delegate int TestDelegate(string s); \/\/ \u30c7\u30ea\u30b2\u30fc\u30c8 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=3584\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Delegate \u30e1\u30e2&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-3584","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3584"}],"version-history":[{"count":6,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3584\/revisions"}],"predecessor-version":[{"id":3604,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3584\/revisions\/3604"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}