Sign in to follow this  
nordwindranger

[.net] passing classes (c#)

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this