46

I have this in my code:

SelectList(blah, "blah", "blah", cu.Customer.CustomerID.ToString())

It gives a error when it returns null, how can I make it CustomerID is an empty string if it is null?

/M

Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109
  • 1
    Is `CustomerID` the null value, or is the `Customer` itself null? Also, what is the type of `CustomerID`? – Thorarin Nov 02 '09 at 09:52

4 Answers4

98

(Update for C# 6.0)

If you are using C# 6 or newer (Visual Studio 2015 or newer), then you can achieve this using the null-conditional operator ?.:

var customerId = cu.Customer?.CustomerId.ToString() ?? "";

One useful property of the null-conditional operator is that it can also be "chained" if you want to test if several nested properties are null:

// ensure (a != null) && (b != null) && (c != null) before invoking
// a.b.c.CustomerId, otherwise return "" (short circuited at first encountered null)
var customerId = a?.b?.c?.CustomerId.ToString() ?? "";

For C# versions prior to 6.0 (VS2013 or older), you could coalesce it like this:

string customerId = cu.Customer != null ? cu.Customer.CustomerID.ToString() : "";

Simply check if the object is non-null before you try to access its members, and return an empty string otherwise.

Apart from that, there are situations where null object pattern is useful. That would mean that you ensure that your Customer's parent class (type of cu in this case) always return an actual instance of an object, even if it is "Empty". Check this link for an example, if you think it may apply to your problem: How do I create a Null Object in C#.

Community
  • 1
  • 1
vgru
  • 49,838
  • 16
  • 120
  • 201
28

(C# 2.0 - C# 5.0)

The ternary operator works, but if you want even shorter expression working on arbitrary objects you can use:

(myObject ?? "").ToString()

Here is real-life example from my code:

 private HtmlTableCell CreateTableCell(object cellContents)
 {
     return new HtmlTableCell()
     {
         InnerText = (cellContents ?? "").ToString()             
     };
 }
Dima Korobskiy
  • 1,479
  • 16
  • 26
  • 2
    One of the outcomes of this code is redundant `"".ToString()`, moreover `(myObject ?? "")` will work only if `myObject` was a string in the first place, making `.ToString()` even more unnecessary. I think you meant `myObject?.ToString() ?? ""`. – Robert Synoradzki Nov 02 '18 at 20:24
  • 2
    @ensisNoctis 1. This was written at the time we had C# 5, which didn't have `?.` operator. For C# 6+, I'd use `?.`. 2. `.ToString()` works on objects just fine. I stand by my answer scoped to C# 2 - C# 5: updated to clarify. – Dima Korobskiy Nov 19 '18 at 17:27
  • Fine with me :) – Robert Synoradzki Nov 20 '18 at 12:05
17

It depends of the type of CustomerID.

If CustomerID is a string then you can use the null coalescing operator:

SelectList(blah, "blah", "blah", cu.Customer.CustomerID ?? string.Empty)

If CustomerID is a Nullable<T>, then you can use:

SelectList(blah, "blah", "blah", cu.Customer.CustomerID.ToString())

This will work because the ToString() method of Nullable<T> returns an empty string if the instance is null (technically if the HasValue property is false).

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
1
SelectList(blah, "blah", "blah", 
(cu.Customer.CustomerID!=null?cu.Customer.CustomerID.ToString():"")
)
Palantir
  • 23,820
  • 10
  • 76
  • 86