{"id":510,"date":"2019-01-08T18:05:43","date_gmt":"2019-01-08T22:05:43","guid":{"rendered":"http:\/\/www.tayloraliss.com\/blog\/?p=510"},"modified":"2019-01-08T18:07:24","modified_gmt":"2019-01-08T22:07:24","slug":"welcoming-the-delegates","status":"publish","type":"post","link":"https:\/\/www.tayloraliss.com\/blog\/?p=510","title":{"rendered":"Welcoming the delegates"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I have started working through <a href=\"https:\/\/www.manning.com\/books\/the-art-of-unit-testing-second-edition?a_bid=f6ea80f5&amp;a_aid=iserializable\">The Art of Unit Testing<\/a> by Roy Osherove and came upon this test that took me a while to understand:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[Test] \npublic void IsValidFileName_EmptyFileName_Throws()\n{\n    var ex = Assert.Catch&lt;Exception>(() => la.IsValidLogFileName(\"\"));                 \n    StringAssert.Contains(\"filename has to be provided\", ex.Message);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I was really confused about what was happening on line 4. Looking at the <a href=\"https:\/\/github.com\/nunit\/docs\/wiki\/Assert.Catch\">NUnit documentation<\/a>, I saw that this is format for the <code>Assert.Catch<\/code> method:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">T Assert.Catch&lt;T>(TestDelegate code);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method is going to return an exception, but it needs to be passed a <code>TestDelegate<\/code> first. We can see the syntax for a <code>TestDelegate<\/code> <a href=\"https:\/\/developer.xamarin.com\/api\/type\/NUnit.Framework.TestDelegate\/\">here<\/a>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public delegate Void TestDelegate ()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This seems strange because according to the code, <code>Assert.Catch<\/code> is being passed a lambda expression: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">() => la.IsValidLogFileName(\"\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Well <a href=\"https:\/\/stackoverflow.com\/a\/167392\">it turns out<\/a> that Lambda expressions are a simpler syntax for anonymous delegates and can be used everywhere an anonymous delegate can be used. The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/delegates-lambdas\">Microsoft docs<\/a> even say as much:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>a lambda expression is just another way of specifying a delegate<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">The question is then, &#8220;what is a delegate?&#8221;  From <a href=\"http:\/\/www.tutorialsteacher.com\/csharp\/csharp-delegates\">my research<\/a>, a delegate is used in C# when you want to pass a function itself as a parameter. You often see people refer to a delegate as &#8220;a pointer to a function.&#8221; The syntax for creating a delegate is as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;access modifier> delegate &lt;return type> &lt;delegate_name>(&lt;parameters>)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And here is some example code using a delegate:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> class Program\n {\n     public delegate void Print(int value);\n\n     static void Main(string[] args)\n     {\n         Print printDel = PrintNumber;\n\n         \/\/ or you can do this:\n        \/\/ Print printDel = new Print(PrintNumber);\n\n        printDel(100000);\n        printDel(200);\n\n        printDel = PrintMoney;\n\n        printDel(10000);\n        printDel(200);\n    }\n\n    public static void PrintNumber(int num)\n    {\n        Console.WriteLine(\"Number: {0,-12:N0}\",num);\n    }\n\n    public static void PrintMoney(int money)\n    {\n        Console.WriteLine(\"Money: {0:C}\", money);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And here is the output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Number: 10,000 \nNumber: 200 \nMoney: $ 10,000.00 \nMoney: $ 200.00 <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s understand what&#8217;s happening here.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"615\" src=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2019\/01\/explanation-2.png\" alt=\"\" class=\"wp-image-534\" srcset=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2019\/01\/explanation-2.png 772w, https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2019\/01\/explanation-2-300x239.png 300w, https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2019\/01\/explanation-2-768x612.png 768w\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>We are declaring a delegate called <code>Print<\/code> that takes an integer as an argument. We create a new instance of this delegate can call it <code>printDel<\/code>.<\/li><li>We point <code>printDel<\/code> at the <code>PrintNumber<\/code> function.<\/li><li>We call our delegate twice, passing it in two different values. It formats the numbers appropriately with commas as specified in the <code>PrintNumber<\/code> method.<\/li><li>We then point <code>printDel<\/code> at the <code>PrintMoney<\/code> function.<\/li><li>We call our delegate two more times and <em>this&nbsp;time<\/em> it formats the numbers for currency as described in the <code>PrintMoney<\/code> function.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">So, in summary, if we use a delegate, we&#8217;re passing a pointer to chunk of code, whereas if we use a lambda, we&#8217;re passing the code directly (and anonymously). Lambdas are more convenient in this case because we don&#8217;t have to declare them or even give them a name.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have started working through The Art of Unit Testing by Roy Osherove and came upon this test that took me a while to understand: I was really confused about what was happening on line 4. Looking at the NUnit documentation, I saw that this is format for the Assert.Catch method: The method is going [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=510"}],"version-history":[{"count":10,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions\/549"}],"wp:attachment":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}