Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C# support the concept of Copy Constructor?

I was asked this question in one of my interviews, but I wasn't able to find out exactly why this concept is not there.

Please let me know.

like image 809
Amit Avatar asked Apr 20 '10 15:04

Amit


People also ask

Why do we use \t in C?

\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.

Why there is no string in C?

Both Java and Python have the concept of a "string", C does not have the concept of a "string". C has character arrays which can come in "read only" or manipulatable. A character array is a sequence of contiguous characters with a unique sentinel character at the end (normally a NULL terminator '\0' ).

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.


1 Answers

It's not built into the language because there is not a reasonable default implementation.

Copy constructors suffer from many of the same ambiguities as cloning. For example, whether you want to make a shallow copy or a deep copy depends on your particular circumstances.

Say you have an Order class with a Customer property. Should its copy constructor create a new customer or point to the original instance? Probably the original instance - but what about Order.Payment?

Worse, even when you do want to perform a deep copy, you may even not be able to create all of your subordinate objects, because their constructors (or comparable factory methods) may not be accessible.

In case that's not enough, this article on Java Design Issues highlights some other problems (like type truncation).

like image 144
Jeff Sternal Avatar answered Sep 20 '22 18:09

Jeff Sternal