<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; refernence</title>
	<atom:link href="http://www.dijksterhuis.org/tag/refernence/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Passing by Reference and Value in C#</title>
		<link>http://www.dijksterhuis.org/passing-by-reference-and-value-in-c/</link>
		<comments>http://www.dijksterhuis.org/passing-by-reference-and-value-in-c/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 10:45:41 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[refernence]]></category>
		<category><![CDATA[value]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=130</guid>
		<description><![CDATA[
C# might look a lot like C++ but that doesn&#8217;t mean that everything is as it appears to be. There are some gotcha&#8217;s that can trip you up.

Passing values to a function or variable is as common as breathing air. In general there are two ways of passing a value:

By value &#8211; the function or [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/reference.jpg" alt="" title="Passing by Value or Reference in C#" width="500" height="295" class="alignnone size-full wp-image-138" /><br />
<em>C# might look a lot like C++ but that doesn&#8217;t mean that everything is as it appears to be. There are some gotcha&#8217;s that can trip you up.</em></p>
<p><span id="more-130"></span></p>
<p>Passing values to a function or variable is as common as breathing air. In general there are two ways of passing a value:</p>
<ol>
<li>By value &#8211; the function or variable receives a copy of the value, and cannot modify the original. </li>
<li>By reference &#8211; the function receives a reference to the original and is able to modify the original. In C++ this is done usually done through pointers (&#038;value).</li>
</ol>
<p>Although pointers still exist in C# you need to tag your code explicitly as &#8220;unsafe&#8221; and that opens many other cans of worms. So at first glance it looks like C# is passing everything by value. There isn&#8217;t a * or &#038; in sight. But this is deceiving.</p>
<p>Only the build in types (int, float, double,string, structs) are passed by value. All classes are passed by reference.</p>
<pre class="brush: c#">
int            a;
int            b;

a  = 42;
b  = a;
a  = 30;

Console.WriteLine(&quot;Passed by value: {0}&quot;, b);
</pre>
<p>As expected this prints &#8220;42&#8243;.</p>
<p>As said, classes are passed by reference. If you are used to C++ forcing your to use a * to reference a pointer the following code might surprise you somewhat:</p>
<pre class="brush: c#">
    class Demo
    {
        int myValue = 0;

        public int Value
        {
            set { myValue = value; }
            get { return myValue;  }
        }
    }

    class MainClass
    {
        public static void Main(string[] args)
        {
            Demo one  = new Demo();
            Demo two = new Demo();

            one.Value = 20;
            two = one;
            one.Value = 30;

            Console.WriteLine(&quot;The value is {0}&quot; , two.Value );
        }
    }
</pre>
<p>This will actually output &#8220;30&#8243;. Two is a reference to one so its contents are equivalent to one. The memory allocated for &#8220;two&#8221; is released as soon as we refer it to one in line 20. </p>
<p>If you are used to C++, it helps to think of this in C++ concepts: </p>
<pre class="brush: c++">
            Demo *one  = new Demo();
            Demo *two = new Demo();

            one-&gt;Value = 20;
            two = one;
            one-&gt;Value = 30;
</pre>
<p><small>Image by <a rel="nofollow" href="http://www.flickr.com/photos/bslavin/">Barbera L Slavin</a></small></p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/passing-by-reference-and-value-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
