Handy extension method for null-coalesing

Today a colleague asked me a question:
“How do you do a null-coalesce operator which will return a property of an object when not null?”
If you’re not familiar with the null coalesce operator it’s the ?? operator and it can be u…


This content originally appeared on Posts on LINQ to Fail and was authored by Posts on LINQ to Fail

Today a colleague asked me a question: “How do you do a null-coalesce operator which will return a property of an object when not null?” If you’re not familiar with the null coalesce operator it’s the ?? operator and it can be used for inline expressions when the test object is null. You use it like so: string test = null; Console.WriteLine(test ?? "The string was null"); So it either returns itself or it returns your value, but what if you want to return a property of the object not itself, well you can’t use the ?


This content originally appeared on Posts on LINQ to Fail and was authored by Posts on LINQ to Fail


Print Share Comment Cite Upload Translate
APA
Posts on LINQ to Fail | Sciencx (2022-12-09T07:11:09+00:00) » Handy extension method for null-coalesing. Retrieved from https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/.
MLA
" » Handy extension method for null-coalesing." Posts on LINQ to Fail | Sciencx - Sunday April 25, 2010, https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/
HARVARD
Posts on LINQ to Fail | Sciencx Sunday April 25, 2010 » Handy extension method for null-coalesing., viewed 2022-12-09T07:11:09+00:00,<https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/>
VANCOUVER
Posts on LINQ to Fail | Sciencx - » Handy extension method for null-coalesing. [Internet]. [Accessed 2022-12-09T07:11:09+00:00]. Available from: https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/
CHICAGO
" » Handy extension method for null-coalesing." Posts on LINQ to Fail | Sciencx - Accessed 2022-12-09T07:11:09+00:00. https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/
IEEE
" » Handy extension method for null-coalesing." Posts on LINQ to Fail | Sciencx [Online]. Available: https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/. [Accessed: 2022-12-09T07:11:09+00:00]
rf:citation
» Handy extension method for null-coalesing | Posts on LINQ to Fail | Sciencx | https://www.scien.cx/2010/04/25/handy-extension-method-for-null-coalesing/ | 2022-12-09T07:11:09+00:00
https://github.com/addpipe/simple-recorderjs-demo