Search code examples
c#linqnull-coalescing-operator

Use of nullable types and null-coalescing operator together in C#?


In the following MSDN Official article on LINQ Outer Join, author(s) are using ? and ?? together in ...select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };. Question: Why not just use ?? as: select new { person.FirstName, PetName = subpet.Name ?? String.Empty }; that, I think, translates to if subpet.Name is null give me empty string. Am I misunderstanding something?

Reason for the Inquiry: When I was using ? and ?? together in my code (explained here) the VS2015 Intellisense was giving me the error also explained in that post.

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

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

public static void LeftOuterJoinExample()
{
    Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
    Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
    Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
    Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };

    Pet barley = new Pet { Name = "Barley", Owner = terry };
    Pet boots = new Pet { Name = "Boots", Owner = terry };
    Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
    Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
    Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

    // Create two lists.
    List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
    List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

    var query = from person in people
                join pet in pets on person equals pet.Owner into gj
                from subpet in gj.DefaultIfEmpty()
                select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };

    foreach (var v in query)
    {
        Console.WriteLine($"{v.FirstName+":",-15}{v.PetName}");
    }
}

// This code produces the following output:
//
// Magnus:        Daisy
// Terry:         Barley
// Terry:         Boots
// Terry:         Blue Moon
// Charlotte:     Whiskers
// Arlene:

Solution

  • As mentioned by @Lee, subpet might be null. So, in calling subpet.Name throw an exception.Hence, ?? check the subpet is null or not, and ? back to the calling .Name property.