473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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
Nov 15 '05 #1
2 1668
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" <op*****@yahoo.com> wrote in message
news:37**************************@posting.google.c om...
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

Nov 15 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:<ek**************@TK2MSFTNGP11.phx.gbl>...
Also, I think that proper design would solve this. What you are
describing is easily solved by the singleton pattern, IMO.


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
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
35
by: GTO | last post by:
I do not believe that C# is the future of C++. I also do not believe that adding two thousand new library functions to the standard library is the future of C++. But what is the future of C++? Is...
9
by: Lyle Fairfield | last post by:
It's confusing. Many people here and elsewhere make many different predictions: There's an introduction mentioning some aspects of this at...
5
by: Tarundeep Singh Kalra | last post by:
Hi, i am a VC++ developer (MFC) and i want to use .NET at its best. But all the samples , tuotorials etc are either in C# or VB.NET. I have certain doubts :- 1). Is Microsoft going to phase...
8
by: Mitchell S. Honnert | last post by:
In the early days of .NET development, I could react to user's hesitancy to download the Framework with reassurance that it would eventually be included in a future service pack. I also pointed...
15
by: cody | last post by:
currently when trying to change a struct returned by a property, the compiler generates an error. so if you try someControl.Location.Y = 10; you get an error which is logical if one...
29
by: VirtualDev | last post by:
What is the future of C++?, and what is the C++0x? and is it really going to include a standard portable libraries for GUI, networking, embedded systems and so on?
161
by: Dan Lenski | last post by:
Hi all, I'm a recent, belated convert from Perl. I work in a physics lab and have been using Python to automate a lot of measurement equipment lately. It works fabulously for this purpose. ...
75
by: kwikius | last post by:
My hunch is my posts to clc++ are disappearing down a hole, so I post here instead. Future of C++ thread on clc++ "Abhishek" <nospam_abhishekpandey@yahoo.comwrote in message...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.