﻿<?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>Programming By A Tool &#187; Referece</title>
	<atom:link href="http://byatool.com/tag/referece/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>By Reference in C# and How to Get Schooled By It</title>
		<link>http://byatool.com/general-coding/by-reference-in-c-and-how-to-get-schooled-by-it/</link>
		<comments>http://byatool.com/general-coding/by-reference-in-c-and-how-to-get-schooled-by-it/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 20:10:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Referece]]></category>
		<category><![CDATA[Stupid]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=114</guid>
		<description><![CDATA[WARNING: If you understand The concept of By Reference, skip the first part of this post: So you have an object, huh? Ok well what does that mean? Basically there is a stack and a heap. When you declare an object, space is made on the stack, basically it&#8217;s a placeholder saying that there will [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #800000;">WARNING: If you understand The concept of By Reference, skip the first part of this post:</span></p>
<p>So you have an object, huh? Ok well what does that mean? Basically there is a stack and a heap. When you declare an object, space is made on the stack, basically it&#8217;s a placeholder saying that there will be something to point.</p>
<pre>    <span style="color: #33cccc;">User</span> someUser;</pre>
<p>Now when you instatiate:</p>
<pre>    someUser = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">User</span>();</pre>
<p>That user Object is put on the heap and that placeholder (reference) on the stack? Well it&#8217;s given a pointer which SURPRISE points to the object on the heap. Now when you pass the object into the method, you aren&#8217;t really passing what&#8217;s on the heap. You are actually copying and passing the pointer. Inside the method, a new entry is thrown on the stack that points to the same object on the heap. This way if you change anything on the object in the method, the changes are reflected outside the method. (Say if you added items to a list in the method) Now this all changes if the object inside the method is reinitialized or repointed:</p>
<pre>    SomeMethod(<span style="color: #33cccc;">User</span> user)
    {
        user = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">User</span>();
        user.UserName = <span style="color: #800000;">"Sean"</span>;
    }</pre>
<p>Remember that user we passed in? It now has nothing to do with the one in the method now. There is now a new object on the heap and &#8220;user&#8221; in the method now points to it. Why did I tell you all of this?</p>
<p><span style="color: #800000;">END REFERENCE EXPLANATION:</span></p>
<p>Say you have this:</p>
<pre>    ...

    <span style="color: #33cccc;">UserList</span> userList = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">UserList</span>();
    <span style="color: #33cccc;">SomeList</span> list = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">SomeList</span>();</pre>
<pre>    FillListFromUserList(list, userList);

    ...

    <span style="color: #0000ff;">void</span> FillListFromUser(<span style="color: #33cccc;">SomeList</span> listToCopyTo, <span style="color: #33cccc;">UserList</span> userList)
    {
       <span style="color: #0000ff;">foreach</span>(<span style="color: #33cccc;">User</span> currentUser <span style="color: #0000ff;">in</span> userList)
       {
          listToCopyTo.Add(userList);
       }
    }</pre>
<p>Easy, and basically at this point list should have the same items as userList. Now say list needs what&#8217;s in userList but in order by UserID. Suppose you did it like this:</p>
<pre>    <span style="color: #0000ff;">void</span> FillListFromUser(<span style="color: #33cccc;">SomeList</span> listToCopyTo, <span style="color: #33cccc;">UserList</span> userList)
    {
       <span style="color: #0000ff;">foreach</span>(<span style="color: #33cccc;">User</span> currentUser in userList)
       {
          listToCopyTo.Add(userList);
       }

       <span style="color: #0000ff;">var</span> sorted = <span style="color: #0000ff;">from</span> user <span style="color: #0000ff;">in</span> userList
                    <span style="color: #0000ff;">orderby</span> user.UserID
                    <span style="color: #0000ff;">select</span> user;

       listToCopy = sorted.ToList();
    }</pre>
<p>Ok so a quick check to see if list now contains items will show it does. Now say you looked at this method and thought it was a little more than needed. You decide to do this:</p>
<pre>    <span style="color: #0000ff;">void</span> FillListFromUser(<span style="color: #33cccc;">SomeList</span> listToCopyTo, <span style="color: #33cccc;">UserList</span> userList)
    {
        listToCopyTo = userList.OrderBy(user =&gt; user.ID);
    }</pre>
<p>Now a quick check to see if list has something in it shows that it doesn&#8217;t. But wait, what the hell? The first one worked and the second is just short hand right? Eh well the problem is how objects are passed. By reference means that it doesn&#8217;t pass the original pointer from the stack but it copies it so that there are two references on the stack that point to the same object on the heap. What does this all mean? Well both</p>
<pre>    listToCopy = sorted.ToList();
 And
    listToCopyTo = userList.OrderBy(user =&gt; user.ID);</pre>
<p>Have now reset the pointer for the reference in the method (listToCopyTo) making it no longer have anything to do with the list outside of it (list). The reason why it gave a false positive on the first one was that I had added to the list within, and therefor to the object the list outside points to. So the changing of the pointer:</p>
<pre>    listToCopy = sorted.ToList();</pre>
<p>would no longer affect the list oustide. However, in the second example I never added to the object they both pointed to before I changed the pointer on the inner. Hense why the second example had nothing in the outer list. To make matters worse I would have noticed this before if the original userList hadn&#8217;t been sorted by ID already.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/by-reference-in-c-and-how-to-get-schooled-by-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

