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…

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 ?
Print Share Comment Cite Upload Translate
CITATION GOES HERE CITATION GOES HERE
Select a language: