String.IsNullOrEmpty()
appears to be an extremely well used method and I find myself wishing there were some shorthand for it. Something like ???
as it would be used in a similar context to the null coalesce operator but be extended to test for empty as well as null strings. I.e.
string text = something.SomeText ??? "Not provided";
What would be your opinions on this? Would it unnecessarily bloat the language? Would it open the floodgates for other mid-level operations to be granted such deep integration with the compiler? Or would it be a useful addition for the language.
Best Answer
Phil Haack blogged about this a while ago. Basically, he suggests an extension method on
string
that lets you doThe extension method is very simple:
He also suggests a version checking for whitespace instead of just empty with the
string.IsNullOrWhitespace()
method from .NET 4, as well as similar extensions for theIEnumerable<T>
interface.He also talks about introducing new operators such as
???
for this, but concludes that it would be more confusing than helpful - after all, introducing these extension methods you can do the same thing, but it's more readable, and it's using shorthand syntax that "everybody already knows".