|
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...
|
|
|
|
|
Java looks OK but I must admit Eclipse put me right off android development!
Have you looked at Android Studio[^]? I've heard some very good things about it - and installed it but not found the time to learn how to play with it yet.
Any good interviews in prospect?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Well I will have a look at Android Studio, Had four(!) interviews so far, nothing really positive from any of them, I remember when I graduated there were no grad jobs. It seems to me that either they want 20+ years or no years experience. When I had 0 years no one would give me a job cos of the 0 years, 5 years was the minimum now I have 5 years I'm too experienced for the role(!) and not experienced enough for the senior roles...
I have too much hardware experience for a pure software role and too much software for a pure hardware role. I am starting to give up!
|
|
|
|
|
Paint.NET[^] is a damn fine and free app that you may wish to look at.
Government is not reason; it is not eloquent; it is force. Like fire, it is a dangerous servant and a fearful master. ~ George Washington
|
|
|
|
|
For some reason, I was never a fan of Paint.NET and always used PhotoFiltre[^].
|
|
|
|
|
Personally, I shy away from most free apps; there are exceptions, though.
|
|
|
|
|
Agreed. My only complaint has been the startup time of the new version; it takes an age to get going.
Software Zen: delete this;
|
|
|
|