Empty strings

  • Thread starter Thread starter Chris Capel
  • Start date Start date
C

Chris Capel

Is there any way to express this more concisely?

if (someString == null || someString == "")
throw new ArgumentException("String must have something in it."); // or
something

What would be perfect would be someString.IsEmpty(), but MS didn't see fit
to include that particular one. Maybe there's something just as good. ??

Chris
 
What would be perfect would be someString.IsEmpty(), but MS didn't see fit
to include that particular one. Maybe there's something just as good. ??

It would have be a static method, something like String.IsEmptyOrNull(string
str)
because someString.IsEmpty() will throw a null reference exception if
someString is null.

As the MS guys did not provide it, you will have do it (we did). You just
have to write some kind of StringHelper class with this static method in it.

Bruno.
 
Chris,

You should probably change the ArgumentException to an
ArgumentNullException, just to be more concise. Otherwise, I would do as
Bruno recommends, and create your own static method.
 
He's also checking if string is zero-length - ArgumentNullException does not
quite fit in that case.

..
Nicholas Paldino said:
Chris,

You should probably change the ArgumentException to an
ArgumentNullException, just to be more concise. Otherwise, I would do as
Bruno recommends, and create your own static method.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris Capel said:
Is there any way to express this more concisely?

if (someString == null || someString == "")
throw new ArgumentException("String must have something in it."); // or
something

What would be perfect would be someString.IsEmpty(), but MS didn't see fit
to include that particular one. Maybe there's something just as good. ??

Chris
 
Just FYI, if you try Whidbey Alpha, you will see String.IsNullOrEmpty is
added in String class.

Gang Peng
[MS]
 
Back
Top