[.net] passing classes (c#)

Started by
1 comment, last by nordwindranger 16 years, 2 months ago
----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]
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.
Excellent.
Thanks, I wanted to double check this before I started writing code.

This topic is closed to new replies.

Advertisement