 |

|
So now that C# 4.0 is being talked about, I was wondering what people thought would be good additions to the language. Sorry if this is a repost, but I went through several pages, and didn't see anything, so...
What I'd frankly love to see would be tuples. Rather than having to use multiple 'out' parameters, you'd just return multiple values:
public int,int MinMax(int[] numbers)
{
int min, max;
return min, max;
}
What do you think? What would be good for the next version?
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|

|
The Powers Collection or a simple hand rolled generic handles tuples, Pair<int, int>
I have always said that developers need to focus on mastering what has been provided in 2.0 before even thinking about adding more candy.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|

|
And I agree. Learn how to use what you have first. And I've used the Pair object before. But being able to specify a list of return values would be nice. Not necessary, no. But nice. The same thing could be said for the ?? operator though. Do you need it? No. But it's definitely nice!
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|

|
I'd love to be able to check if say, myObject was null and if it was, return a result, and if not, return a property of myObject using the ?? operator.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|

|
Wait, that may have come out wrong but that is something I really would love to have.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|

|
I saw someone comment on that on another forum. Basically, you'd have something like this (using his sample syntax):
int? x = Company?.Person["Bob"]?.Age;
If Company or Company.Person["Bob"] were null, then x would be set to null, rather than getting an exception. I likes.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|

|
That's the best and most practical suggestion for c# I've seen. has anybody raised it to MS?
|
|
|
|
|

|
Jamie Nordmeyer wrote: I saw someone comment on that on another forum.
Daniel Grunwald, TheCodeKing and myself came up with that here[^]. You're missing the final part tho, which is a ?? operator to act as the "default":
int? x = Company?.Person["Bob"]?.Age ?? null;
Of course setting null as the "default default" would also work and be handy.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Check out my blog! )
|)””’) piHole.org
-”-”-
|
|
|
|

|
Actually, the posting I saw wasn't on CodeProject. It may still have been yours, but it wasn't here. Either way, it's a fantastic idea, and I really like it!
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|

|
Wasn't likely mine, then--that's the only place I remember discussing it; but it could have been one of the other guys. On the other hand it could have been someone else entirely, which only goes to show it must be a good idea if people are coming up with it independently! If you do find the link, let me know; it would be interesting to read what other people suggested.
It would definitely be nice to have in an upcoming version of C#.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Check out my blog! )
|)””’) piHole.org
-”-”-
|
|
|
|

|
Lol I just realized this morning as I got up how dumb that was of me last night. You didn't forget anything--null would of course be the default, that's the whole point. :P "?? null" is redundant.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Check out my blog! )
|)””’) piHole.org
-”-”-
|
|
|
|
|

|
There is an extension of C# available that provides some help in this area: spec#.
I haven't used it myself, but I'm a big fan of Eiffel, which has deep design-by-contract support. Spec# includes notation to indicate that a variable or parameter can never be null.
BTW, if you're really interested in further reading on features for C#, the Eiffel language is a good place to start, as a language that has true generics since inception as well as tuples, anchored types, parameter covariance and many other interesting goodies.
|
|
|
|

|
Actually, I think the whole problem would be better solved by lifting the . operator in the same way they did for some of the other operators to handle nullable types. It is essentially making nullability a true first-class language citizen, whereby accessing a property or calling a function on a null object simply returns null rather than a NullReferenceException.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|

|
Tuple support is certainly nice but there are other ways to handle this. Simply having some predefined Tuple<T> type classes like we have with Func<T> and Action<T> would be more than sufficient, I think.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|

|
Ennis Ray Lynch, Jr. wrote: I have always said that developers need to focus on mastering what has been provided in 2.0 before even thinking about adding more candy.
Would it really be hard to "master" tuples as a return paramter? I love that feature as part of LUA and Python especially over out parametes.
Spec# sounds interesting which adds support for explicit programming by contract.
Todd Smith
|
|
|
|

|
It is more that .NET 2.0 provide the exact construct as required to perform the action without the need for a language update. I am not saying it is hard to master but I am constantly seeing instances of persons requesting features that are already supported.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|

|
Todd Smith wrote: Spec# sounds interesting which adds support for explicit programming by contract.
Yea!
Kevin
|
|
|
|
|

|
I'd love to see a const keyword on parameters to methods, and optional parameters. Both of which seem simple enough.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|

|
here here!
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|

|
I put these things to the C# team at least twice, and they appeared too stupid to understand why they were of value.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|

|
I strongly Agree!
C# cut-offs from c++ were really too deep. cons keyword (both on parameters and members signatures) shoud be a MUST in any oo language.
And what about the annoying lack of default in parameters? I'd like to see c# designers and gurus dealing with Office PIA... I'm sure they would have a private build of csc.exe with default parameters implementeed .... InvokeExcelStuff(theOnlyNeededParameter, null, null, null, null, null, null, null, null, null, null, null, null, null, null....)
|
|
|
|

|
Giampaolo Papotti wrote: And what about the annoying lack of default in parameters? I'd like to see c# designers and gurus dealing with Office PIA... I'm sure they would have a private build of csc.exe with default parameters implementeed .... InvokeExcelStuff(theOnlyNeededParameter, null, null, null, null, null, null, null, null, null, null, null, null, null, null....)
This should become simpler...stay tuned.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|

|
hear, hear!
I had heard that proper run-time const support in the clr was far too hard or expensive, and providing a weak const keyword in c# (like c++ has got) without properly enforcing it in the clr would just be misleading.
Shame though; I would have liked proper const guarantees in the clr.
|
|
|
|

|
Why const? What will it even do besides limit the programmer in the usage of said parameters?
|
|
|
|

|
In a language where most things are passed by reference, there's even more value in an interface making an explicit promise to not alter an object that it is given to work with.
How does it limit the programmer ? If you want to alter an object, don't mark it const.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|

|
Well, if you Don't want to alter it, why would you care to tell your compiler that? This is not C++ or C where it would have made a significant difference in some cases.
|
|
|
|

|
Because if you're providing an interface, you provide a contract with the people who use that interface. If I write a library, I can use const to tell a user when they can trust my code not to change their stuff.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|

|
Christian Graus wrote: I can use const to tell a user when they can trust my code not to change their stuff.
erm, I don't see how that would work.
I thought that const was more of a promise to yourself, or to implementors, not to users. Nothing prevents you from calling methods and setting fields, right?
Ok, let me start it again.
What is the const keyword supposed to do in parameters that has an effect outside the method?
"Computer Science is no more about computers than astronomy is about telescopes." - Edsger Dijkstra
|
|
|
|

|
Here's an example of what I think he's saying:
public void DoSomething(const Employee emp)
{
emp.Age = 32; }
An object is passed by reference, so its properties are settable. You don't want to make the Age property read only, because normally, you want the consumer to be able to set it. But what if DoSomething was meant to be a final validation, and you didn't want anything in the object to change, because it could mess up state elsewhere? The const keyword would allow the developer to leave a property or field writable, but still be able to restrict when it could be written to.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|

|
Ok, I think I get it.
So, I would be able to call methods and get properties and fields, but forbidden from setting fields and properties.
Well, there are still the methods to change the data, so I guess that optimization-wise, this doesn't help.
So, it's mostly syntactic sugar, a contract constraint. I'd put it in the same category as varargs, in terms of usefulness.
You say that VB.NET has this feature today? How does it work when you import a VB.NET library to another language, like C#?
"Computer Science is no more about computers than astronomy is about telescopes." - Edsger Dijkstra
|
|
|
|

|
harold aptroot wrote: Why const? What will it even do besides limit the programmer in the usage of said parameters?
Well, that's kind of the point. You want to limit the usage of const parameters to minimize side-effects.
|
|
|
|

|
So you limit yourself - how about promising yourself not to alter it without writing it down? Saves time and space.
|
|
|
|

|
Because it will end like most of promises you (easily) give to yourself, it will almost certainly be broken.
|
|
|
|

|
Then so be it, in this case you would simply remove the "const" if it were there, meaning that it shouldn't really have been there to start with
|
|
|
|

|
Do you never write code that other people will use ?
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|

|
Can you remove the code if you have to satisfy an interface definition that contains the const keyword?
I've heard more said about less.
|
|
|
|
|

|
harold aptroot wrote: Sure, edit the interface
And when its in a third party assembly? Are you proposing to decompile it through reflection, edit, then rebuild it to use just to omit the const? And if its obfuscated?
I've heard more said about less.
|
|
|
|

|
This is the const would be a bad idea.
|
|
|
|

|
I disagree.
This is the reason for const. To constrain an implementation. You think its a bad idea because you can't subvert it.
Hmmm... while we're at it we might as well eliminate private and protected aspects of classes as well. Get rid of readonly and just let everything be completely open. And watch the bugs fly...
Do you have a solid argument against const?
I've heard more said about less.
|
|
|
|

|
shiftedbitmonkey wrote: Do you have a solid argument against const?
Of course not, I just don't think that the reasons to include it are strong enough, so I argue
|
|
|
|

|
Leslie Sanford wrote: You want to limit the usage of const parameters to minimize side-effects.
Shouldn't it be "You want to use the limit of const parameters to minimize side-effects"?
|
|
|
|

|
1. one of main differences between high-level languages and assembly language is that they introduces many ways to limit the programmer
2. const keyword is not only a limitation it is also a reminder for you and for others that there is a reason why something should not be changed. And const is certainly a better solution than running around the office saying 'promise me that you will not try to change data returned by SomeLongAndCrypticFunctionName'
|
|
|
|

|
Mladen Jankovic wrote: And const is certainly a better solution than running around the office saying 'promise me that you will not try to change data returned by SomeLongAndCrypticFunctionName'
Yes, but running is good for you!
To hell with circumstances; I create opportunities.
|
|
|
|

|
const parameters would be very nice,
it's a very clear way to show me that my object has not been changed after the function to which I passed it returns
|
|
|
|

|
Christian Graus wrote: optional parameters
Ditto - one of the few things that I prefer in VB.NET. Creating loads of overloads is a royal PITA.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|

|
Yes, it's something the compiler could easily do for you.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
 |