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 / .NET Framework / CLR / February 2005

Tip: Looking for answers? Try searching our database.

Copy constructor for structs.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Varn - 24 Feb 2005 21:02 GMT
It is my understanding that structs are value types in .NET and are not
passed by reference.  However, if the struct contains objects, they will
still be copied by reference.  Is there any way to provide a copy
constructor for a struct that will automatically clone any objects that are
contained within it?

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Jon Skeet [C# MVP] - 24 Feb 2005 22:08 GMT
> It is my understanding that structs are value types in .NET and are not
> passed by reference.  

They can be passed by reference, if you choose to. Note that there's a
big difference between passing a value by reference and passing a
reference by value. See
http://www.pobox.com/~skeet/csharp/parameters.html

> However, if the struct contains objects, they will
> still be copied by reference.  Is there any way to provide a copy
> constructor for a struct that will automatically clone any objects that are
> contained within it?

You could certainly create a constructor for a struct that takes
another struct of the same type - but that won't be called when the
struct is passed.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Ken Varn - 25 Feb 2005 16:22 GMT
So are you saying that there is no way for a structure to be able to
automatically clone members that are objects if the structure is passed by
value?  In C++, a copy constructor takes care of this quite nicely.  Is
there no equivalent in .NET?

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

> > It is my understanding that structs are value types in .NET and are not
> > passed by reference.
[quoted text clipped - 12 lines]
> another struct of the same type - but that won't be called when the
> struct is passed.
Jon Skeet [C# MVP] - 25 Feb 2005 17:38 GMT
> So are you saying that there is no way for a structure to be able to
> automatically clone members that are objects if the structure is passed by
> value?  

Indeed I am. (Or rather, members are cloned, but only at the shallowest
level.)

> In C++, a copy constructor takes care of this quite nicely.  Is
> there no equivalent in .NET?

No, there isn't - at least not in C#, as far as I know. Managed C++
*might* have something similar.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Rate this thread:







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.