Null coalescing operator (??) not useful

I still haven't found a good use for the C# ?? null coalescing operator. I always find that I want a property of the possibly null object, not the object itself. I wish it worked with this code:

Person p = null;
string name = p.Name ?? string.Empty;

That is, it should take the left-hand side if any part of the right-hand side is null. Now that would be useful.

Here's the best I could come up with for making use of it:

class Person
{
    public string Name { get; set; }

    public static Person Default = new Person();
}

Person p = null;
string name = (p ?? Person.Default).Name ?? string.Empty;

Not exactly intention revealing.

Leave a Reply

You must be logged in to post a comment.