
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.
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