static string DemoTheNewWayOfNullChecking() { Contact contact = new Contact(); contact.Name = "Sean"; contact.Phone = "12345678"; contact.Age = 24; //contact.PersonalPet = new Pet() //{ // PetName = new PetName() // { // Name = "My Pet" // } //}; // Maybe class allow you to do something similar to int? for the reference types. Maybe<Contact> maybeContact = new Maybe<Contact>(contact); var maybePetName = from c in maybeContact from cp in c.PersonalPet from pn in cp.PetName select pn.Name; if (maybePetName.HasValue) return maybePetName.Value; return null; }
static string DemoTheOldWayOfNullChecking() { Contact contact = new Contact(); contact.Name = "Sean"; contact.Phone = "12345678"; contact.Age = 24; //contact.PersonalPet = new Pet() //{ // PetName = new PetName() // { // Name = "My Pet" // } //}; if (contact == null) return null; if (contact.PersonalPet == null) return null; if (contact.PersonalPet.PetName == null) return null; var petName = contact.PersonalPet.PetName.Name; if (petName == null) return null; return petName; }