473,791 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1682
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.co m

"not_a_comm ie" <no********@gma il.comwrote in message
news:0f******** *************** ***********@k2g 2000hse.googleg roups.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********@gma il.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.co m>
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(__make ref(s));
}

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

Alun Harford
Jan 12 '08 #4

"not_a_comm ie" <no********@gma il.comwrote in message
news:0f******** *************** ***********@k2g 2000hse.googleg roups.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***@NoShitMa il.comwrote:
>
"not_a_comm ie" <no********@gma il.comwrote in message
news:0f******** *************** ***********@k2g 2000hse.googleg roups.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.co m>
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_comm ie" <no********@gma il.comwrote in message
news:94******** *************** ***********@q77 g2000hsh.google groups.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
3351
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
6701
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
1545
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 inputinfor a, ref
2
2517
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 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below): *** Start listing 1 *************************************************** // test1.cpp
3
1417
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
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10155
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.