|
I, too, share your enthusiasm or, perhaps I daresay, I do even surpass it!
Oops, misread it, I mean I am enthusiastic! legit!
modified 15-Sep-20 22:19pm.
|
|
|
|
|
Some nice stuff overall from what I've seen (new() and delegate* unmanaged ), but I really don't understand how a lot of the pattern matching updates in the last few C# releases are supposed to be useful. Why do we need a is not null when we've had a != null forever (and it's shorter to type)?
|
|
|
|
|
It helps prevents null reference exception at compile time
|
|
|
|
|
Does it? I can't test C#9 at the moment but for the normal equality comparison:
bool F(object x) => x is null;
ldarg.1
ldnull
ceq
ret
bool F(object x) => x == null;
ldarg.1
ldnull
ceq
ret
If it was a compile-time check there would be no need to do anything except ldc.i4.1 or ldc.i4.0 in the body of the resulting IL. I imagine the is not and != decompile to something similar.
Or maybe I'm misunderstanding you
|
|
|
|
|
what I will say is just an educated guess since I din't investigate it closely...
I don't think they change the runtime for that.. method signature is the same...
So.. not only there is a compile time check provided by this new syntax, but the compile also produce IL code which verify the parameters as well so that it accepts only non null (or nullable), as specified...
i.e. your method that requires non null parameter will never throw a null reference exception (from the argument anyway) even if called with, say, reflection / dynamic or such like...
|
|
|
|
|
Jon McKee wrote: Why do we need a is not null when we've had a != null forever (and it's shorter to type)?
It's been added for consistency. What's new in C# 9.0 #pattern-matching-enhancements[^]
Personally I feel that instead of using (MyVariable != null) or (MyVariable is not null) or any variations thereof, one could use (MyVariable is object)
I'm not very fond of negations.
Wrong is evil and must be defeated. - Jeff Ello
Never stop dreaming - Freddie Kruger
|
|
|
|
|
I like the variable is object notation And yea, I guess it's a byproduct of adding not that we get yet another way to test for null.
Some TIL if interested:
I've been playing around with the various ways to perform these checks and it turns out it's all stylistic choice from an IL perspective[1]. Any check equivalent to != null gets translated to ldnull and cgt.un (including is object ) and any check equivalent to == null gets translated to ldnull and ceq .
[1]: Except if the equality operator is overloaded in which case is forces the default equality operator (and the IL above) whereas == and != use the overloaded operator via call bool X::op_Inequality(class X, class X) .
modified 16-Sep-20 3:44am.
|
|
|
|
|
The best part is that (MyVariable is object) can be enhanced to (MyVariable is MyClass) and now we have a really proper sanity check
Wrong is evil and must be defeated. - Jeff Ello
Never stop dreaming - Freddie Kruger
|
|
|
|
|
To be fair that might be a little confusing for the uninitiated. Checking that a variable/parameter is the type it's defined as looks weird.
|
|
|
|
|
And normally it wouldn't be necessary either, C# being a typed language and all that.
But there are cases when using inheritance or interfaces when it makes sense.
I find it's a good habit though.
Wrong is evil and must be defeated. - Jeff Ello
Never stop dreaming - Freddie Kruger
|
|
|
|
|
Jörgen Andersson wrote: I'm not very fond of negations. It's the double negations masquerading as affirmations trip me up.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I'm never not against those negations... um...
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
What's wrong with the way C++ does it?
if (myobj) {
}
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
#realJSOP wrote: What's wrong with the way C++ does it?
Nothing really.
Wrong is evil and must be defeated. - Jeff Ello
Never stop dreaming - Freddie Kruger
|
|
|
|
|
BillWoodruff wrote: And they express strong conviction the equally improved wheels will be more, or less, round.
Let's see if their wheels can surpass these: https://3.bp.blogspot.com/-O-dkHHKj72E/TdzNH5YDgqI/AAAAAAAABfM/nIUTwjL_NR8/s1600/mn01.jpg[^]
Anyway, if they're going that route, it'll mean that once they have the wheels, the next step would be to design the road those wheels can run on
(But it's not like this topic hasn't been covered before: (PDF) Roads and Wheels[^] )
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
modified 16-Sep-20 6:43am.
|
|
|
|
|
|
That just needs to move on. I supposed to go to the Florida panhandle Friday.
|
|
|
|
|
That's really cool. Thanks for posting.
|
|
|
|
|
What's that storm forming off the coast of Yucatan?
|
|
|
|
|
Throwing sodium chloride into someone's eyes is a crime – it's a salt.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'll have to keep an ion the responses to this one, I'm sure some will rock.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Although some might say it's best to table the discussion, else there's bound to be a whole lotta shakin' goin' on. Which might relieve my boredom - I've got a lot of brine on my hands.
/ravi
|
|
|
|
|
A crystal clear response from a seasoned professional!
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
jeron1 wrote: a seasoned professional Ha ha, that's sodium funny!
/ravi
|
|
|
|
|
Ravi Bhavnani wrote: Although some might say it's best to table the discussion, The old classic, Ricks: Blunders in International Business[^] (recommended!) tells a story about some serious cooperation problems in a joint British/US project. Before one meeting agreed in advance that these problems "be tabled". The Brits thought that they had agreed to bring the problems up on the table, "face up", for discussion and resolution. The US guys were similarly convinced that they had agreed to lay the problems "face down", dead, on the table, not making the problems a topic for this meeting.
The conflicting understanding of "to table something" did not reduce the cooperation problems.
|
|
|
|