Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Copy constructors in C#!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Atmapuri - 11 Feb 2008 09:45 GMT
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
Jon Skeet [C# MVP] - 11 Feb 2008 09:58 GMT
> Would the optimizer in C# be able to take advantage of the copy constructor:
>
[quoted text clipped - 10 lines]
> 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
Atmapuri - 11 Feb 2008 18:41 GMT
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
Jeroen Mostert - 11 Feb 2008 19:08 GMT
Please leave in a record of who wrote what.

> Jon Skeet wrote:
>> 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.

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.

Signature

J.

Jon Skeet [C# MVP] - 11 Feb 2008 19:29 GMT
> > 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

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?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ben Voigt [C++ MVP] - 11 Feb 2008 21:00 GMT
>>> When you use assignment with structs, it does a bitwise copy -
>>> literally just copies a chunk of memory. How could a copy
[quoted text clipped - 4 lines]
> 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.

>> or deep copying contained objects.
>
> What makes you think it performs deep copies at the moment?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.