473,403 Members | 2,354 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,403 software developers and data experts.

C# should support "const ref"

A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.
Jan 12 '08 #1
8 1659
Not a bad idea. Have you submitted it to the Product Feedback site? A
link to your entry would be helpful, so people can vote on it.

Don't be surprized if there is resistance (from inside MS) to this, as
this would require a CLR change (I can't think of a way to do it through
"compiler magic") and history has shown that they are resistant to that (the
CLR hasn't changed since .NET 2.0).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"not_a_commie" <no********@gmail.comwrote in message
news:0f**********************************@k2g2000h se.googlegroups.com...
>A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.
Jan 12 '08 #2
not_a_commie <no********@gmail.comwrote:
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.
Do you really need such large structs in the first place? Just make
them classes (keeping them immutable) and you'll be fine.

--
Jon Skeet - <sk***@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
Jan 12 '08 #3
not_a_commie wrote:
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.
How would you implement the compiler? It's hard, even before you
consider unmanaged code. To construct a pretty hard example:

public void Problem2(const ref MyStruct s)
{
Modifier(__makeref(s));
}

private void Modifier(TypedReference tr)
{
__refvalue(tr, MyStruct) = new MyStruct();
}

Alun Harford
Jan 12 '08 #4

"not_a_commie" <no********@gmail.comwrote in message
news:0f**********************************@k2g2000h se.googlegroups.com...
>A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.
I think the thought process was that const being static would conflict with
ref's variable change.
Jan 13 '08 #5
Chizl <Ch***@NoShitMail.comwrote:
>
"not_a_commie" <no********@gmail.comwrote in message
news:0f**********************************@k2g2000h se.googlegroups.com...
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.

I think the thought process was that const being static would conflict with
ref's variable change.
Well, that's just a matter of naming. It could easily have been
"readonly" instead of "const".

--
Jon Skeet - <sk***@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
Jan 13 '08 #6
I think the thought process was that const being static would conflict with
ref's variable change.

Well, that's just a matter of naming. It could easily have been
"readonly" instead of "const".
I like readonly as well. C++ uses const so I thought we might as well
go with something familiar.
Jan 14 '08 #7
I stuck this on MS Feedback: #322420
Jan 15 '08 #8

"not_a_commie" <no********@gmail.comwrote in message
news:94**********************************@q77g2000 hsh.googlegroups.com...
>I stuck this on MS Feedback: #322420
Sadly, not likely to happen.

All the C++ developers are already pissed at having const-correctness
broken, and Microsoft already knows it quite well (some of their own
developers blogged about it IIRC). But const-correctness is something you
can't retrofit.
Jan 16 '08 #9

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

Similar topics

22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
5
by: Tee | last post by:
Hi guys, I am confused about the usage of "ref". Please see this code: private void button3_Click(object sender, System.EventArgs e) {
1
by: geri.gan | last post by:
I have C API just like this: enum void getinfor(const struct inputinfor *a, const struct outputinfor ** b) i use p/invok to translate it to internal static extern void getinfor(ref...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
3
by: =?Utf-8?B?U3VybWVldCBKYW5kdQ==?= | last post by:
Hi, Can any body tell me that instead of "const" what can we use in MC++. Thanks in advance!!
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.