Jasper... The real point is that a copy constructor is essential to C++,
but not essential to C#.
Since C++ is deterministic, objects appear like rabbits everywhere. The
assignment operator may call the copy constructor in C++ for proper
behavoir at deallocation. This is essential in C++ to avoid having two
variables representing the same object, each resulting in a call to the
destructor when each variable goes out of scope. The assignment
operator does _not_ call a copy constructor in C#. C# is not
deterministic, so the assignment operator may simply assign two
variables to the same object in memory. The object in memory may be
reclaimed when the object is no longer reachable, so that the
"destructor" is only called once.
http://www.geocities.com/jeff_louie/OOP/oop5.htm
Chapter 5 "C++ and Java Gumption Traps"
4) The Assignment Operator Does Not Call the Copy Constructor
This one really confuses a lot of coders. In C# the assignment operator
simply assigns a reference variable to an existing object, to a new
object, or to null. After the assignment, the reference variable
contains a
reference to an object or to no object (is null). The assignment
operator
does not call the copy constructor to create a new object. It is quite
legal
in C# to have two reference variables that contain references to the
same
object. The two variables occupy different memory locations on the
stack, but contain values that point to the same memory address on the
heap. Both references would have to be released (e.g. the reference
variables go out of scope, be reassigned, set to null) before the object
can be reclaimed. As a result, the "destructor" will only be called
once.
The following code simply creates two references to the same object:
MyClass c1= new MyClass();
MyClass c2;
c2= c1;
bool isSameReference= (c1 == c2); // c1 and c2 contain references to
the same object on the heap
Console.WriteLine(isSameReference.ToString()); // output--> true
Be clear that if variable c1 contains the only reference to an object,
setting c1 to null or reassigning c1 to another object will make the
original object unreachable and eligible for garbage collection.
Regards,
Jeff
Does the concept "copy constructor" from c++ excist in
c#. What is the syntax.<