Copy constructors in C#!

A

Atmapuri

Hi!

Would the optimizer in C# be able to take advantage of the copy constructor:

http://msdn2.microsoft.com/en-us/library/ms173116.aspx

written by the programmer, when copying structs? The copy constructor
can namely copy less in a more efficient way. Example (continuing from
the source above), if the Person type would be struct with copy constructor
would that copy constructor be called in situation like this:

person a2 = new person();
person a = a2;

This is not applicable to classes, but makes every sense with struct
types, which require that constructor is called to initialize them also.

Thanks!
Atmapuri
 
J

Jon Skeet [C# MVP]

Would the optimizer in C# be able to take advantage of the copy constructor:

http://msdn2.microsoft.com/en-us/library/ms173116.aspx

written by the programmer, when copying structs? The copy constructor
can namely copy less in a more efficient way. Example (continuing from
the source above), if the Person type would be struct with copy constructor
would that copy constructor be called in situation like this:

person a2 = new person();
person a = a2;

This is not applicable to classes, but makes every sense with struct
types, which require that constructor is called to initialize them also.

When you use assignment with structs, it does a bitwise copy -
literally just copies a chunk of memory. How could a copy constructor
be more efficient than that?

Jon
 
A

Atmapuri

Hi!
When you use assignment with structs, it does a bitwise copy -
literally just copies a chunk of memory. How could a copy constructor
be more efficient than that?

By not copying all the fields or deep copying contained objects.

Thanks!
Atmapuri
 
J

Jeroen Mostert

Please leave in a record of who wrote what.
By not copying all the fields or deep copying contained objects.
If you need a deep copy, use a class and implement ICloneable. It's never
the default, so that's not an issue.

Not copying all the fields would run counter to the idea of a copy
constructor... Even if you wanted this for a legitimate reason, separately
copying fields would be slower than letting the framework do a single block
copy, unless the fields you want to skip are very large. In which case,
you've got a very odd struct that you probably shouldn't be "copying" in the
first place. Just create a new one explicitly and assign its fields
explicitly, so everybody knows what you're doing.

Note that a C# "copy constructor" is not at all comparable to the C++ copy
constructor. The latter is a part of the language, the former is just
another constructor (and not in any way special). In particular, assuming
'b' is of a type compatible with T, then

T a = b;

will never invoke any constructor in C#,

T a(b);

is a syntax error, and

T a = new T(b);

will not compile unless you've actually written a constructor that takes an
argument that's type-compatible with T.
 
J

Jon Skeet [C# MVP]

Atmapuri said:
By not copying all the fields

That sounds like a very odd usage - and selected copying is likely to
be slower than just blitting.
or deep copying contained objects.

What makes you think it performs deep copies at the moment?
 
B

Ben Voigt [C++ MVP]

Jon said:
That sounds like a very odd usage - and selected copying is likely to
be slower than just blitting.

Especially since the non-selected fields have to be initialized to
something, C# objects aren't allowed to contain uninitialized data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top