{"id":518,"date":"2003-10-02T13:23:45","date_gmt":"2003-10-02T17:23:45","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=518"},"modified":"2003-10-02T13:23:45","modified_gmt":"2003-10-02T17:23:45","slug":"out-method-parameter-in-c","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/10\/02\/out-method-parameter-in-c\/","title":{"rendered":"out method parameter in C#"},"content":{"rendered":"<p>Just discovered this C# tidbit called the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfOut.asp\">out method parameter<\/a>.  Let&#8217;s say you have a method:<\/p>\n<p>public String ReplaceAll(String toReplace, String replaceWith)<\/p>\n<p>and you want to know how many replacements were actually made.  With a regular method you can only return the modified string.  The out method parameter gives you the ability to return another variable. The modified method would look like this:<\/p>\n<p>public String ReplaceAll(String toReplace, String replaceWith, out int numReplaced)<\/p>\n<p>and then concretely:<\/p>\n<p>String myString = &#8220;aaron&#8221;;<br \/>\nint replaced;<br \/>\nmyString.ReplaceAll(&#8220;a&#8221;, &#8220;k&#8221;, out replaced);<br \/>\nConsole.WriteLine(&#8220;The method ReplaceAll() replaced &#8221; + replaced + &#8221; characters.&#8221;);<\/p>\n<p>Similar idea in the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfRef.asp\">ref method parameter<\/a>, but you have to initialize the variable before sending it to the method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just discovered this C# tidbit called the out method parameter. Let&#8217;s say you have a method: public String ReplaceAll(String toReplace, String replaceWith) and you want to know how many replacements were actually made. With a regular method you can only return the modified string. The out method parameter gives you the ability to return another &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/10\/02\/out-method-parameter-in-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">out method parameter in C#<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/518"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}