I want to know, if I have a class named Test, what’s the difference below
Test test = new Test();
Test newTest = test;
Test newTest2 = test.Clone();
What’s the different between newTest and newTest2?
Anyone can help?
Thanks in advance!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you assign the instance, if
Testis a class, you’re actually copying the reference, buttestandnewTestwill both point to the same instance in memory.This means that both variables point to the same object:
Clone(), on the other hand, typically is used to indicate a copy of the object itself, which means thattestandnewTest2would point to different objects, so the above wouldn’t happen.Note that, if test is a
struct(value type), then a direct assignment is effectively a full (shallow) copy of the object, by value.