|
Agreed, this is a really nice syntax extension (yes its ugly), and may even help reduce the number of null references exceptions.
More stuff like this, and less of the async/await type stuff please.
Regards,
Rob Philpott.
|
|
|
|
|
I like both. I've worked on many projects that used IAsyncResult and the async/await syntax would have really really helped me out.
|
|
|
|
|
..yes, you're right, but, I'm going to miss the NullReferenceException. I liked that annoying little fella!
But I am obviously going to try this out. It is a short form of the try catch block for the NullReferenceException.
Favourite line: Throw me to them wolves and close the gate up. I am afraid of what will happen to them wolves - Eminem
~! Firewall !~
|
|
|
|
|
Nice, indeed.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
|
It can be useful, but also very bad. Think about such code in your sample, with NullReferenceException you had a way to know where the chain broken...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Not without putting it on separate lines. I've found myself doing hacks with either the ternary operator, or
var a = (obj ?? new Obj()).Value
or other ugly hacks just to make things a little more succinct than nesting a bunch of if statements to get the same effect.
|
|
|
|
|
When you had the punishment of NullReferenceException hang over your neck you would not dare to write all that in a single line anyway, but checking each and every part for existence an validity...IMHO it's a much better way - 'fail with grace' I think...
The new syntax is ugly and can get you into serious problems when need to be debugging...
The only sure thing - it will increase Q&A income
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
I can see that you don't like this at all, and it doesn't fit the style of code that you write. I'm sharing something that works for me.
However, there is plenty of code I write where this is exactly the behavior I need. There are many forms that I deal with where null signifies something that isn't filled out, and I currently have to write extra code that is harder to read.
|
|
|
|
|
Hi KEP,
I would assume that a statement like the one shown by the OP would be followed by a test for a 'null result, possibly deliberately throwing an error if the result was 'null ... at least in the development stage. Once a 'null result is detected, one could set a break-point, and easily determine which operation returned null ?
This is, of course, speculation
cheers, Bill
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
|
|
|
|
|
You assume to much - we have developers do not check for null even today. Imagine one learned to develop with the new syntax...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
A definite keystroke saver!
/ravi
|
|
|
|
|
That's exactly what I thought when I came across that too. At some point, I was actually considering implementing this, even though performance wouldn't be great:
var zip = StopAtNull<string>(() => customers.FirstOrDefault().Orders.FirstOrDefault().Address.Zip);
In fact, I think I saw a tip/trick along those lines. I'm glad to see this made it into the language itself.
|
|
|
|
|
So that would just wrap it in a try/catch(NullReferenceException) { return null; }?
Yeah, I assume the built in version would check at each operator which would probably be faster in most cases. Your method would also swallow null reference exceptions thrown from with functions which might or might not be what you want.
|
|
|
|
|
The idea was that it would use expression trees to break apart each call and check for nulls at each step.
|
|
|
|
|
Oh, that's cool. I sometimes forget how easy it is to do magic like that.
|
|
|
|
|
(disclaimer: I'm not a C# developer.)
Nice!
but I have a question.
What kind of error/message do you get when one of the chain "member" returns null ?
Is it easy to debug and maintain ?
I'd rather be phishing!
|
|
|
|
|
Maximilien wrote: What kind of error/message do you get when one of the chain "member" returns null ?
I think you missed the part that I copied where it stated that if any of the references evaluates to null, then the entire expression evaluates to null.
Maximilien wrote: Is it easy to debug and maintain ?
I haven't used it. I'm just looking forward to using it. I assume you can hover over parts of it just like any other C# expression, or you can use the immediate window. Is that what you are asking?
|
|
|
|
|
|
When I read the title, I expected to see John's name but...no John.
|
|
|
|
|
Brilliant! I've got to make me a Denial of Service Dog!
Will Rogers never met me.
|
|
|
|
|
From Apollo Robin[^], one of the best I have listened to yet.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
Entropy isn't what it used to.
|
|
|
|
|
|
...and *gulp* bought a Corel product.
Specifically, Corel PaintShop Pro X6 (it was a gooood offer!) to replace my beloved - but aged - JASC PaintShop Pro 9, of 2004 vintage.
Oh boy, is it different...I can't even get the toolboxes arranged sensibly...time to RTFM
I may be some time...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
You have my sympathy, I am trying to learn my around Eclipse (& Java) should be no trouble, I think not...
|
|
|
|