   [.net] C# - returning a ref?
  Hey!In the following code snippet, shouldn't add() return a reference of the object this? Cause after entity = item, this still contains its default value. public class EntityCollection : List<E2D_Entity>{   new public void Add( E2D_Entity item )   { E2D_Entity entity = add( item ); if ( entity != null )    entity = item;   }   private E2D_Entity add( E2D_Entity item )   { for ( int i = 0; i < this.Count; i++ ) {    if ( this.IsDead )    return this; } base.Add( item ); return null;   }}Thanks
  Add returns the value that was stored at this. Because E2D_Entity (strange naming btw) happens to be a class, this value is either null or a reference to an object of type E2D_Entity. Add will, however, not return a reference to the slot this itself.;  How come that this works then?    public partial class Form1 : Form    {   TC test = new TC();   private void Form1_Load( object sender, EventArgs e )   {  Test ts = new Test();  test.Add( ts );  test.get();   }    }    public class Test    {   public bool check = true;    }    public class TC : List<Test>    {   public void get()   {  Test ts = get2();  ts.check = false;   }   public Test get2()   {  return this[0];   }    }   ;  To me it looks like you're trying to replace an element at  and you want to be doing it like soif ( this.IsDead ){this = item;return this;}Your second snippet works the same as the first, you're not changing what this[0] points to but the contents themselves.The code below in effect does nothing, because you're assigning to a scoped variable and when it leaves the scope, nothing will happen. In essence, what VizOne said.E2D_Entity entity = add( item );if ( entity != null )entity = item; ;Quote:Original post by Niksan2To me it looks like you're trying to replace an element at  and you want to be doing it like soWhy didn't I think about that before? xPQuote:Your second snippet works the same as the first, you're not changing what this[0] points to but the contents themselves.The code below in effect does nothing, because you're assigning to a scoped variable and when it leaves the scope, nothing will happen. In essence, what VizOne said.Thanks, but something still confuses me. The second snippet get2() returns this[0] to get(), if it is like you said, the scoped variable ts in get() shouldn't be able to change this[0] content as you said that get2() couldn't return a ref to this[0].;Quote:Original post by R4z0r1991Thanks, but something still confuses me. The second snippet get2() returns this[0] to get(), if it is like you said, the scoped variable ts in get() shouldn't be able to change this[0] content as you said that get2() couldn't return a ref to this[0].That's because it doesn't return this[0] but the Test class that this[0] points to, and you can change it because ts is pointing to the same bit of memory that this[0] is.The same way the following works:Test t = new Test();Test t2 = t;t2.check = false;t.check and t2.check would be false;   ;  His just overriting the variable.new public void Add( E2D_Entity item ){E2D_Entity entity = add( item ); //entity now holds a reference to the object at thisif ( entity != null )entity = item;  //entity now holds a reference to the object referenced by item, nothing have happend to this}You cant overwrite a object by doingE2D_Entity var1 = new E2D_Entity();E2D_Entity var2 = new E2D_Entity();var2 = var1;You only overwrite the variable var2. Sure you now lost the object initialy used for var2 but the memory it occypied didnt get overwritten with a copy of the object at var1.hope that made sense[/source][Edited by - Lio on April 20, 2010 5:21:11 AM];Quote:Original post by LioHis just overriting the variable.*** Source Snippet Removed ***It is not correct to use the term "address" the way you did in that snippet. Rather than say 'holds the address of' you instead say 'is a reference to.'   ;Quote:Original post by jpetrieQuote:Original post by LioHis just overriting the variable.*** Source Snippet Removed ***It is not correct to use the term "address" the way you did in that snippet. Rather than say 'holds the address of' you instead say 'is a reference to.'Yes i see your point. Now fixed :)   