• Advertisement
Sign in to follow this  

[.net] passing classes (c#)

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

----language is C#----- I have run into a situation where I unforunately need two seperate lists that reference the same objects. From what I understand of C#, classes are automatically passed as references so...
class foo()
{
}

class fooCollector()
{
 list<foo> list1 =new list<foo>();

 public void addFoo(foo item)
 {
  list1.add(item);
 }
}

class fooOriginator()
{
 private foo newFoo=new foo();
 list<foo> list2=new list<foo>()

 private void someMethod()
 {
  list2.add(newFoo);
  fooCollectior.addFoo(newFoo)
 }
}
this code would result in both lists having a refence to the same foo object right? Or does it create two seperate foo objects? thanks [Edited by - nordwindranger on February 1, 2008 4:11:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
Yes, they should both refer to the same Foo. You can also check this by calling Object.Equals() which compares object identity if you don't override it to do something different for your derived types.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement