4

Actually i didn't understand the concept of it, meaning that why and when it should be employed. Usually, we can assign values to an instance of a class but why we should send an object to another object like this :

 private void button8_Click(object sender, EventArgs e) { rectangle r1 = new rectangle(50, 70); rectangle r2 = new rectangle(r1); } class rectangle { private int length; private int breadth; public rectangle(rectangle x) { length = x.length; breadth = x.breadth; MessageBox.Show("I'm a Copy Constructor. \n Length= " + length.ToString() + "\n breadth= " + breadth.ToString()); } } 
3
  • It´s a simple way to copy your rectangle. From the outside this is not possible because your properties are private. Also you could modify your rectangle or something like that. Commented Feb 27, 2016 at 20:29
  • Well, I may have seen it used in some libraries but that's not a common sight in C#. Copy constructors are the obvious cornerstone of C++ programming, but in C# they are nothing more than a fancy(but proper) way to organize deep cloning( at least that what I would expect from it). Commented Feb 27, 2016 at 20:35
  • Does this answer your question? Copy constructor versus Clone() Commented Dec 22, 2022 at 10:43

2 Answers 2

1

A copy constructor is a concept from borrowed from C++: the only purpose of this constructor is to make a copy from the existing object. Additionally, the compiler defines the copy constructor in C++ automatically (implicit copy constructor) if the programmer does not provide one. And you can mix: for classes with special copy behaviour, you provide the copy constructor. For other classes, the implicit copy constructor calls your customized one, if the class has members of this type.

Summary: the copy constructor in C++ is a simple way to copy objects and it members. The trick is that you don't have to provide one explicitly and it is supported at compiler level.

As far as I know, there are no implicit copy constructors in C#. Instead of this special constructors, you can use cloning in C#. See this question, for example.

Another note: structs are copied on assigment, but not recursive. Even there is no implicit copy constructor, its only a simple memory copy.

For C++ copy constructors, see section 12.8, page 288 in the C++14 standard for example.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a possible approach to fully or partially initialize an object copying members' values from the source object.

In your case, you can call this as mapping objects.

Alternatively, rectangle class could implement a static method like CreateFrom that would work as the current constructor and it would also work like a factory method:

class rectangle { private int length; private int breadth; public static rectangle CreateFrom(rectangle x) { rectangle r = new rectangle(); r.length = x.length; r.breadth = x.breadth; return r; } } 

...and maybe this way you find your event handler code more interesting:

private void button8_Click(object sender, EventArgs e) { rectangle r1 = new rectangle(50, 70); rectangle r2 = rectangle.CreateFrom(r1); } 

1 Comment

shouldn't CreateFrom have a return r?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.