Connecting Tech Pros Worldwide Forums | Help | Site Map

Idea for future version of C#: Move semantics for structs

Samuel Barber
Guest
 
Posts: n/a
#1: Nov 15 '05
Consider a struct that resembles this:

struct mystruct
{
byte[] myarray;
// ...other stuff...
}

i.e. a struct that contains at least one reference/pointer.

The meaning of "=" (and the meaning of by-value parameter passing) is
to perform a memberwise copy of the struct. This is also known as a
"shallow copy", because it doesn't copy heap data (myarray in the
example above).

This behavior is hazardous, because a shallow copy isn't really an
object copy (clone). The objects can get into an inconsistent state,
unless they are read-only.

What is desired is for the source object to be "destroyed" when a copy
is done, so that a copy leaves only one usable object - making it a
safe move operation, as opposed to a dangerous copy. This can be
accomplished by simply zeroing/nulling the members of the source
object after the copy. (In the case where the compiler can determine
statically that the source object is never used again, the zeroing can
be optimized away).

Some people might think, "What's the point? Just use a class." But
structs will be more useful in C#2.0 than they are now, because you'll
be able to store them directly in collections (no boxing).

I'd like to be able to say something like:
move struct mystruct {....}

and get the move semantics described above.

Sam

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Idea for future version of C#: Move semantics for structs


Samuel,

I don't think that this is needed, to be honest. While I respect the
use situation that you have presented, I think that it is not that common to
introduce a new language element.

Also, I think that proper design would solve this. What you are
describing is easily solved by the singleton pattern, IMO.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com


"Samuel Barber" <opendtv@yahoo.com> wrote in message
news:37991aef.0310090445.424e7f79@posting.google.c om...[color=blue]
> Consider a struct that resembles this:
>
> struct mystruct
> {
> byte[] myarray;
> // ...other stuff...
> }
>
> i.e. a struct that contains at least one reference/pointer.
>
> The meaning of "=" (and the meaning of by-value parameter passing) is
> to perform a memberwise copy of the struct. This is also known as a
> "shallow copy", because it doesn't copy heap data (myarray in the
> example above).
>
> This behavior is hazardous, because a shallow copy isn't really an
> object copy (clone). The objects can get into an inconsistent state,
> unless they are read-only.
>
> What is desired is for the source object to be "destroyed" when a copy
> is done, so that a copy leaves only one usable object - making it a
> safe move operation, as opposed to a dangerous copy. This can be
> accomplished by simply zeroing/nulling the members of the source
> object after the copy. (In the case where the compiler can determine
> statically that the source object is never used again, the zeroing can
> be optimized away).
>
> Some people might think, "What's the point? Just use a class." But
> structs will be more useful in C#2.0 than they are now, because you'll
> be able to store them directly in collections (no boxing).
>
> I'd like to be able to say something like:
> move struct mystruct {....}
>
> and get the move semantics described above.
>
> Sam[/color]


Samuel Barber
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Idea for future version of C#: Move semantics for structs


"Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com> wrote in message news:<ekyJejmjDHA.1408@TK2MSFTNGP11.phx.gbl>...[color=blue]
> Also, I think that proper design would solve this. What you are
> describing is easily solved by the singleton pattern, IMO.[/color]

I'm afraid I didn't communicate well, since "singleton pattern" is
completely unrelated. The intent of nulling source objects (when a
memberwise copy is performed) is not to limit the number of instances
(it doesn't do that), but rather to make it safer to use structs with
reference members. There is no way to implement it in the language,
because C# gives no control over object copying.

Sam
Closed Thread


Similar C# / C Sharp bytes