{"id":132,"date":"2016-02-18T11:05:30","date_gmt":"2016-02-18T11:05:30","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=132"},"modified":"2022-02-17T06:24:15","modified_gmt":"2022-02-17T06:24:15","slug":"reference","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/memory\/pointers\/reference","title":{"rendered":"Reference"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Pass A Reference To A Function, Method, etc<\/h4>\n\n\n\n<p>Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment.<\/p>\n\n\n\n<p>You just use the &#8216;ref&#8217; or &#8216;out&#8217;&nbsp;keyword:<\/p>\n\n\n\n<p>&#8216;ref&#8217; tells the compiler that the object is initialized before entering the function<\/p>\n\n\n\n<p>&#8216;out&#8217; tells the compiler that the object will be initialized inside the function<\/p>\n\n\n\n<p>So &#8216;ref&#8217; is two-ways and &#8216;out&#8217; is out-only<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic void MyMethodName(ref int&#91;] BookIds, ref string&#91;] BookLanguages)\n\t{\n\t\/\/or:\n\tpublic void MyMethodName(out int&#91;] BookIds, out string&#91;] BookLanguages)\n\t{<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Calling the method<\/h4>\n\n\n\n<p>You need to use the &#8216;ref&#8217; or &#8216;out&#8217; keyword here too<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyMethodName(ref TheBookIds, ref TheBookLanguages);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Pass A Reference To A Function, Method, etc Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment. You just use the &#8216;ref&#8217; or &#8216;out&#8217;&nbsp;keyword: &#8216;ref&#8217; tells the compiler that the object is initialized before entering the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,29],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-methods","category-pointers"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":421,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/132\/revisions\/421"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}