What Is the Difference Between assigning value to new object and object ? [Resolved]

Posted by Kasani007 under C# on 2/2/2017 | Points: 10 | Views : 579 | Status : [Member] | Replies : 1
what is difference between assigning value to new object and object instance..explain?

Student std=new Student();
std=studentDomainObject;


and

Student std=studentDomainObject;





Responses

Posted by: Rajnilari2015 on: 2/2/2017 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kasani007 Sir, there is no difference. Because, in both the case we are assigning the studentDomainObject object to std object. So both the objects are pointing to the same memory block as they are reference types.

So if we write, the output will be same

studentDomainObject.Age=10;
studentDomainObject.Name="test";

Student std=new Student();
std=studentDomainObject;

Console.WriteLine("studentDomainObject.Age = {0},studentDomainObject.Name ={1} ", studentDomainObject.Age,studentDomainObject.Name);
Console.WriteLine("std.Age = {0},std.Name ={1} ", std.Age,std.Name);


Output
-------

studentDomainObject.Age = 10 ,studentDomainObject.Name=test
std.Age = 10 ,std.Name=test



OR

studentDomainObject.Age=10;
studentDomainObject.Name="test";
Student std=studentDomainObject;
Console.WriteLine("studentDomainObject.Age = {0},studentDomainObject.Name ={1} ", studentDomainObject.Age,studentDomainObject.Name);
Console.WriteLine("std.Age = {0},std.Name ={1} ", std.Age,std.Name);


Output
-------

studentDomainObject.Age = 10 ,studentDomainObject.Name=test
std.Age = 10 ,std.Name=test


Note:~ If however, you want to make std a completely different entity from studentDomainObject, you need to do the same by implementing the IClonable interface (though not recommended by MS Team https://msdn.microsoft.com/en-us/library/system.icloneable(v=vs.110).aspx )
or by using a copy constructor .

Hope that helps.


--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response