Passing Objects

HI All,

This is a simple question, i m confused about.

I am passing an object from one class to another like,


\\ Make Object of Class3 - suppose obj3
\\ Call Class2 passing object of Class3

Class2 secondClass = new Class2(obj3);

Now in Class2.cs constructor

private Class3 objof3;

Class2(Class3 anObject)
objof3 = anObject;
// Some Stuff

I want to use the object values of anObject throughout my Class2 class.
So, i am making private object of class3 in Class2 itself. and assign that objof3 to the anObject object.

Now the question is,
If i am assigning it this way, then will objof3 will contain the reference to the anObject object, or it will copy everything in a new memory location.

as far as i know, objects are reference types, and so it will take only the reference of the anObject class, and i will be able to use the anObject values throughout my Class2 through the objof3 object, is it right??

I just want to clear my doubt.
Who is Participating?
monkeymoleConnect With a Mentor Commented:
Yes you are right with the reference model you describe. You will only store a refernce to the object in Class2. Therefore if you created multiple Class2's with the same instance of Class3 object, any changes to the instance used to create the Class2s would be seen in all the Class3's.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.