2009-12-14

String.IsNullOrEmpty

Instead of

    if( null == myString || string.Empty == myString ){...

one can use

    if( string.IsNullOrEmpty( myString ) ){...

There is nothing magic about it, just a more readable, and hence less error prone, syntax.

It would be nice of someone could solve the

    if( null == myCustomer.Address || null == myCustomer.Address.City || null == myCustomer.Address.City.Name ){...

-problem.

In Dotnet4 there will be a method similar to IsNullOrEmpty which also checks for whitespace.  It should come in handy.

2 comments:

Claes Mogren said...

I like the ?.-operator in groovy.

if(null == myCustomer?.Address?.City?.Name){

This would be true if any of myCustomer, Address, City or Name was null, without throwing a nullpointer exception. Something for C# 5?

mblund said...

Claes: Of course that you was the first one to comment this :-)

In Java we have to wait until v8
But hey, you don't pass around lots of null values anyway, do you?