3

In C++, we have Copy Constructor, Destructors, overloaded = which are together called copy control.

My questions are:

  1. Is copy constructor used in C# for initializing objects when passed to a function as argument or when initializing (not assigning) or when returning an object from a function as in C++?

  2. Does an implicitly overloaded = operator function gets called when we assign (not initialize) any object to another object of the same type?

4 Answers4

1

In C# you cannot overload the assignment operator.

Destructors don't really make sense in a managed memory world (most of the time). There is actually an equivalent (finalizers) but you should very rarely need to utilize them.

Copy constructors are made for certain classes, but it's not done nearly as often as in C++. It will never be called implicitly by the language, it will only be used when you manually call it.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • thx for your ans..i want to know what `c#` does when we pass an object as an argument to a function..if `copy constructor` is **not** used then how does it `initialize` the object in its `parameter`.. –  Dec 13 '12 at 15:10
  • For objects, the handle gets copied (ie pass-by-reference). For value types, the object itself gets deep-copied (ie pass-by-value). You can force pass-by-reference for value types using `ref` or `out`. – Blindy Dec 13 '12 at 15:12
  • @Blindy That is not true. **All** parameters in C# are passed by value, in which the bytes of the variable are copies (for reference types that variable is just a reference to another object, but that reference is still copied by value). You can explicitly pass **any** variable by reference, in which you have a reference instead of a copy to that variable. – Servy Dec 13 '12 at 15:14
  • You just said the same thing I did, objects->handle gets copied, value->it all gets copied. – Blindy Dec 13 '12 at 15:19
  • @Blindy You stated that reference types are passed by reference by default. They are not, **all** variables are passed by value, it's just that for reference types that variable is itself a reference. The mechanism of passing parameters is still by value though. Additionally, you can pass **any** parameter by reference, not just value types. Understanding the difference between passing a value by reference and a reference by value is important. – Servy Dec 13 '12 at 15:22
1
  1. No: reference objects are passed by reference, while value objects are copied byte-for-byte. You can make your own "copy constructors", but you would be responsible for calling them explicitly.
  2. You cannot overload the assignment operator

In fairness to C#, none of the intricacies of the C++ copy control are necessary because of the underlying garbage-collected memory model. For example, you do not need to control the ownership of dynamically allocated objects when copying objects, because you are free to have as many references to dynamic objects as you wish.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @cSharper By default, it copies the bytes in that variable. You can also pass a parameter by reference, in which you are given a reference to the variable that was passed in. – Servy Dec 13 '12 at 15:11
  • @cSharper What happens when you pass an object as a parameter depends on its type: if it's a "by-reference" type (i.e. a `class`), then a reference is passed; otherwise, when the object is a value type (i.e. a `struct` or a primitive) then its data is copied byte-for-byte. – Sergey Kalinichenko Dec 13 '12 at 15:13
  • @cSharper If the object is a `class`, the reference gets copied. This is very similar to passing by reference in C++. If you pass a `struct` without a `ref` keyword, the entire content of the `struct` will be copied. – Sergey Kalinichenko Dec 13 '12 at 15:14
1

No. If you want to provide copying mechanism, you do this by implementing the ICloneable interface: http://msdn.microsoft.com/en-us/library/system.icloneable.aspx

Stefan
  • 4,166
  • 3
  • 33
  • 47
  • 1
    `ICloneable` has the issue that the contract is not very clear. It is not recommended that you implement it. See this answer: http://stackoverflow.com/a/699221/517852 – Mike Zboray Dec 13 '12 at 15:08
1

C# has no concept of a copy constructor, nor of overloading operator=. In C# objects just exist and you use handles to them in your code. Handles can be copied "by value" to be used throughout your code, but that's a so-called "shallow" copy, the object itself is still the same, they all point to the same memory (indirectly).

Copy constructors are akin to deep copying in the managed world, and you can achieve that through ICloneable, but it's completely manual and up to the object's implementation to write it. You can also achieve it through serialization (the boost way) or through any manner of indirect ways.

As a final point, since object lifetimes are non-deterministic (they die when the GC arbitrarily decides they should die), there's no such thing as destructors either. The closest thing are finalizers (called non-deterministically when your object gets collected) and the IDisposable pattern (along with using) which give you a semblance of control over finalization. Needless to say they are rarely used.

Edit: I should point out that, while copy constructors have no equivalent, you do have "type casting" constructors through implicit, the precise name escapes me at the moment.

Blindy
  • 65,249
  • 10
  • 91
  • 131