{"id":57,"date":"2008-02-27T15:37:03","date_gmt":"2008-02-27T19:37:03","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=57"},"modified":"2008-02-27T17:26:49","modified_gmt":"2008-02-27T21:26:49","slug":"c-delegate","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=57","title":{"rendered":"C# Delegate"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<ul>\n<li>Delegate defines a function signature<\/li>\n<li>Delegate is a place holder for a future function<\/li>\n<li>Delegate is an object<\/li>\n<ul>\n<li>Need to be instantiated before being used<\/li>\n<li>Can be passed around like an object<\/li>\n<\/ul>\n<\/ul>\n<p><strong>Define a simple delegate<\/strong><\/p>\n<ul>\n<li>\n<pre lang=\"csharp\">\r\n\/\/ Define a delegate for fxn signature\r\npublic delegate \r\n  void SwapItems(\r\n  ref string str1, ref string str2);\r\n\r\n\/\/ Example class\r\npublic class MySwapClass {\r\n  \/\/ Define a conforming method \r\n  public void MySwapFxn(\r\n    ref string str1, ref string str2){\r\n    \/\/ Implementation\r\n  }\r\n\r\n  \/\/ Use delegate as parameter\r\n  public void SortItems(\r\n    string[] items, SwapItems fxn){\r\n    fxn(items[0], items[1]);\r\n  }\r\n  \r\n  \/\/ Run\r\n  public static void Main()\r\n  {\r\n    MySwapClass msc = new MySwapClass();\r\n    \/\/ Construct a delegate object\r\n    SwapItems myswapfxn = new SwapItems(\r\n      msc.MySwapFxn);\r\n    \/\/ Items to sort\r\n    string[] items = {\"one\", \"two\"};\r\n    \/\/ Pass delegate object\r\n    msc.SortItems(items, myswapfxn);\r\n  }\r\n}\r\n\r\n<\/pre>\n<\/li>\n<\/ul>\n<p><strong>Define a multicast delegate<\/strong><\/p>\n<ul>\n<li>\n<pre lang=\"csharp\">\r\n\/\/ Define a multicase delegate\r\n\/\/ Need to be a void return type\r\npublic delegate void MyDelegate();\r\n\r\n\/\/ Class using delegate\r\npublic class DelegateConsumer\r\n{\r\n  \/\/ Delegate object\r\n  public MyDelegate md;\r\n\r\n  \/\/ Constructor\r\n  public DelegateConsumer()\r\n  {\r\n  }\r\n\r\n  \/\/ Invoke delegate\r\n  public void InvokeDelegate()\r\n  {\r\n    if (md != null)\r\n    {\r\n      md();\r\n    }\r\n  }\r\n}\r\n\r\n\/\/ Handlers\r\npublic class MyDelegateHandlers\r\n{\r\n  public static void Handler1()\r\n  {\r\n    Console.WriteLine(\r\n      \"Invoking Handler1...\");\r\n  }\r\n\r\n  public static void Handler2()\r\n  {\r\n    Console.WriteLine(\r\n      \"Invoking Handler2...\");\r\n  }\r\n}\r\n\r\n\/\/ Test run\r\npublic class TestMulticastDelegate\r\n{\r\n  public static void Main()\r\n  {\r\n    DelegateConsumer dc = \r\n      new DelegateConsumer();\r\n    MyDelegate d1 = new MyDelegate(\r\n      MyDelegateHandlers.Handler1);\r\n    MyDelegate d2 = new MyDelegate(\r\n      MyDelegateHandlers.Handler2);\r\n    dc.md += d1;\r\n    dc.md += d2;\r\n    dc.InvokeDelegate();\r\n  }\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview Delegate defines a function signature Delegate is a place holder for a future function Delegate is an object Need to be instantiated before being used Can be passed around like an object Define a simple delegate \/\/ Define a &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=57\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-V","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}