Use coalescing operator to write more readable code

Tired of code like this?

public string Foo

{

get { return foo; }

set

{

if (value == null)

value = String.Empty;

foo = value;

}

}

public string Bar

{

get { return bar; }

set

{

if (value == null)

value = String.Empty;

bar = value;

}

}

Use coalescing operator to make your code a bit more readable.

public string Foo

{

get { return foo; }

set

{

foo = value ?? String.Empty;

}

}

public string Bar

{

get { return bar; }

set

{

bar = value ?? String.Empty;

}

}

Does this look better? The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand. More information on ?? can be found at http://msdn.microsoft.com/en-us/library/ms173224.aspx