tags:

views:

96

answers:

2

Hi.

i have a collection of object. when i add some object on that collection. will it save that object itself or save its reference.

List<Student> myList;
Student std1 = new Student();
Student std2 = new Student();
myList.Add(std1);
myList.Add(std2);

myList will contain what? duplicate copy of std1 and std2 or reference of these two objects?

and if this List resides on some remote location and i access it via wcf service. then i add objects in this list. what will happen. Those objects will be added by MBV or MBR?

actually i have List ok and i am adding object of Student in this list..

in last I said that suppose I have this List in some Program which is on other machine. I can access that program using WCF Service. get the reference of that Program through WCF Service. and then i call Program.myList.Add(student). now will a duplicate copy will be generated on that remote machine and will be added to myList or reference will be saved?

+1  A: 

You're using a List<Object> and Object is a reference type, so your list will hold references to instances.

I am not sure what you mean about the last part, could you please elaborate.

Brian Rasmussen
actually i have List<Student> ok and i am adding object of Student in this list..in last I said that suppose I have this List in some Program which is on other machine. I can access that program using WCF Service. get the reference of that Program through WCF Service. and then i call Program.myList.Add(student). now will a duplicate copy will be generated on that remote machine and will be added to myList or reference will be saved?
Mohsan
Doesn't WCF serialize objects and send them over?
Svish
Okay, is Student a class or a struct? If it is a class and thus a reference type, then your list will still hold references. If it is a struct you list will hold the values of the Student instances.As for the marshalling issue: MarshalByRefObject gives you a proxy, and the framework will update the original. You are not holding an actual reference to the original. For serilization you get a copy and changes you make to this are not reflected in the original.
Brian Rasmussen
Student is a class.
Mohsan
A: 

Because the Object type is a reference type, the List in the example will store reference values to the objects allocated as Student objects on the managed heap.

Wael Dalloul