title: C# elegant way to check if a property's property is null
body: <p>In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null.</p>
  
  <p>ObjectA.PropertyA.PropertyB.PropertyC</p>
  
  <p>How can I get PropertyC safely with the least amount of code?</p>
  
  <p>Right now I would check:</p>
  
  <pre><code>if(ObjectA != null &amp;&amp; ObjectA.PropertyA !=null &amp;&amp; ObjectA.PropertyA.PropertyB != null)
  {
      // safely pull off the value
      int value = objectA.PropertyA.PropertyB.PropertyC;
  }
  </code></pre>
  
  <p>It would be nice to do something more like this (pseudo-code).</p>
  
  <pre><code>int value = ObjectA.PropertyA.PropertyB ? ObjectA.PropertyA.PropertyB : defaultVal;
  </code></pre>
  
  <p>Possibly even further collapsed with a null-coalescing operator.</p>
  
  <p><strong>EDIT</strong> Originally I said my second example was like js, but I changed it to psuedo-code since it was correctly pointed out that it would not work in js.</p>