{"id":939,"date":"2012-05-22T14:41:43","date_gmt":"2012-05-22T12:41:43","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=939"},"modified":"2012-05-22T14:44:38","modified_gmt":"2012-05-22T12:44:38","slug":"passing-a-method-as-parameter-in-c","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/05\/passing-a-method-as-parameter-in-c\/","title":{"rendered":"Passing a &#8216;Method&#8217; as parameter in C#"},"content":{"rendered":"<p>This is actually nothing new but sometimes you might still need a quick reference for to remember how to do this quickly&#8230; Say you have a generic class that does something like generating a report and display it somehow (or save or whatever the class do) and want all the &#8216;calling classes&#8217; to just pass the &#8216;Method&#8217; that it must &#8216;run&#8217; in order to generate the report. Yes there are probably nice and fancy tools these days that could also do it if you have the mulla for it&#8230;<\/p>\n<p>A solution is to use Anonymous methods that has been with C# (.Net) since version 3 of something.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nReporter r = new Reporter();\r\nr.Report = delegate\r\n{\r\nstring stuff = &quot;example report yada yada yada&quot;;\r\nreturn stuff;\r\n}\r\nr.Show();\r\n<\/pre>\n<p>The generic class that provides the &#8216;generic&#8217; functionality can then look something like this using a defined delegate so it knows what output it will receive when &#8216;running&#8217; the method.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic delegate string RefreshActionDelegate();\r\nclass Reporter\r\n{\r\n    public RefreshActionDelegate Report { get; set; }\r\n\r\n    public Show()\r\n    {\r\n       if (Report != null)\r\n       {\r\n          string theContent = Report();\r\n          DoSomethingWithContent(theContent);\r\n       }\r\n    }\r\n}\r\n<\/pre>\n<p>This is just a quick example but it shows how you can use the main &#8216;generic&#8217; class from multiple places each time just passing a different &#8216;Method&#8217; with its own functionality.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is actually nothing new but sometimes you might still need a quick reference for to remember how to do this quickly&#8230; Say you have a generic class that does something like generating a report and display it somehow (or &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/05\/passing-a-method-as-parameter-in-c\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,188,8,9,303,77,189,190,24],"class_list":["post-939","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-anonymous","tag-c","tag-code","tag-development","tag-method","tag-parameter","tag-passing","tag-tip"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=939"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/939\/revisions"}],"predecessor-version":[{"id":941,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/939\/revisions\/941"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}