|
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.
|
|
|
|
|
Cumin, do we really have Thyme to be Peppered with these questions?
|
|
|
|
|
|
Giving someone a stack of Zinc and Copper discs is also a crime - it's a battery.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I though someone would tear into you for that, you know - shaker things up a bit.
Question: is a celibate furniture making[^] sailor a salt shaker ?
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|