{"id":47,"date":"2014-07-28T07:38:12","date_gmt":"2014-07-28T07:38:12","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=47"},"modified":"2014-07-28T07:38:12","modified_gmt":"2014-07-28T07:38:12","slug":"invoking-methods-using-named-parameters-in-c","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=47","title":{"rendered":"Invoking Methods Using Named Parameters in C#"},"content":{"rendered":"<p><strong>Invoking Methods Using Named Parameters<\/strong><br \/>\nAnother language feature found in C# is support for named arguments. To be honest, at first glance, this\u00a0language construct might appear to do little more than result in confusing code. And to continue being\u00a0completely honest, this could be the case! Similar to optional arguments, including support for named\u00a0parameters is largely motivated by the desire to simplify the process of working with the COM\u00a0interoperability layer.<br \/>\nNamed arguments allow you to invoke a method by specifying parameter values in any order you\u00a0choose. Thus, rather than passing parameters solely by position (as you will do in most cases), you can\u00a0choose to specify each argument by name using a colon operator. To illustrate the use of named\u00a0arguments, assume we have added the following method to the Program class:<br \/>\nstatic void DisplayFancyMessage(ConsoleColor textColor,<br \/>\nConsoleColor backgroundColor, string message)<br \/>\n{<br \/>\n\/\/ Store old colors to restore after message is printed.<br \/>\nConsoleColor oldTextColor = Console.ForegroundColor;<br \/>\nConsoleColor oldbackgroundColor = Console.BackgroundColor;<br \/>\n\/\/ Set new colors and print message.<br \/>\nConsole.ForegroundColor = textColor;<br \/>\nConsole.BackgroundColor = backgroundColor;<br \/>\nConsole.WriteLine(message);<br \/>\n\/\/ Restore previous colors.<br \/>\nConsole.ForegroundColor = oldTextColor;<br \/>\nConsole.BackgroundColor = oldbackgroundColor;<br \/>\n}<br \/>\nNow, the way DisplayFancyMessage() was written, you would expect the caller to invoke this method<br \/>\nby passing two ConsoleColor variables followed by a string type. However, using named arguments, the<br \/>\nfollowing calls are completely fine:<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\nConsole.WriteLine(&#8220;***** Fun with Methods *****&#8221;);<br \/>\n&#8230;<br \/>\nDisplayFancyMessage(message: &#8220;Wow! Very Fancy indeed!&#8221;,<br \/>\ntextColor: ConsoleColor.DarkRed,<br \/>\nbackgroundColor: ConsoleColor.White);<br \/>\nDisplayFancyMessage(backgroundColor: ConsoleColor.Green,<br \/>\nmessage: &#8220;Testing&#8230;&#8221;,<br \/>\ntextColor: ConsoleColor.DarkBlue);<br \/>\nConsole.ReadLine();<br \/>\n}<br \/>\nOne minor gotcha regarding named arguments is that if you begin to invoke a method using\u00a0positional parameters, they must be listed before any named parameters. In other words, named\u00a0arguments must always be packed onto the end of a method call.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Invoking Methods Using Named Parameters Another language feature found in C# is support for named arguments. To be honest, at first glance, this\u00a0language construct might appear to do little more than result in confusing code. And to continue being\u00a0completely honest, this could be the case! Similar to optional arguments, including support for named\u00a0parameters is largely motivated by the desire to&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=47\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=47"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}