Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:11:38+00:00 2026-05-27T02:11:38+00:00

Consider this simple c# example: var person = new Person {Name = Fred, MailingAddress=null

  • 0

Consider this simple c# example:

var person = new Person {Name = "Fred", MailingAddress=null };
var result = String.Format("{0} lives at {1}",person.Name, person.MailingAddress.Street);

clearly this will throw a NullReferenceException because the MailingAddress proptery is null.

I could rewrite the second line as:

var result = String.Format("{0} lives at {1}", person.Name, person.MailingAddress == null ? (String)null : person.MailingAddress.Street);

Is there a simpler way to say express this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:11:39+00:00Added an answer on May 27, 2026 at 2:11 am

    There’s not really any good syntax for this. The coalesce operator is part of it, but you need to handle traversing through a null, not just replacing a null. One thing you could do would be to have a static “null object” for the class, something like:

    public class Address 
    {
      public static Address Null = new Address();
      // Rest of the class goes here
    }
    

    Then you could use the coalesce operator like so:

    (person.MailingAddress ?? Address.Null).Street
    

    If you want to go the extension method route, you could do something like this:

    public static class NullExtension
    {
        public static T OrNew<T>(this T thing)
            where T: class, new()
        {
            return thing ?? new T();
        }
    }
    

    Then you could do:

    (person.MailingAddress.OrNew().Street)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simple example. Class A { B b; A() { this.b = new
Consider this simple example: template <class Type> class smartref { public: smartref() : data(new
Consider this simple example; <?php $text = test; ?> <script type=text/javascript defer=defer> var test;
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Consider this simple example (which displays in red): echo -e \033[31mHello World\033[0m It displays
Consider this simple XML document. The serialized XML shown here is the result of
I'm designing an HTML page for display in Android browsers. Consider this simple example
Consider this simple xml element example: <parent foo=1 bar=2 foobar=3> <child/> </parent> In the
Please consider this simple example. I can't get the text of the state element
I'm missing something obvious with the format section of as.Date. Consider this example d1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.