title: C# "as" cast vs classic cast
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/496096/casting-vs-using-the-as-keyword-in-the-clr">Casting vs using the ‘as’ keyword in the CLR</a>  </p>
  </blockquote>
  
  
  
  <p>I recently learned about a different way to cast. Rather than using</p>
  
  <pre><code>SomeClass someObject = (SomeClass) obj;
  </code></pre>
  
  <p>one can use this syntax:</p>
  
  <pre><code>SomeClass someObject = obj as SomeClass;
  </code></pre>
  
  <p>which seems to return null if obj isn't a SomeClass, rather than throwing a class cast exception.</p>
  
  <p>I see that this can lead to a NullReferenceException if the cast failed and I try to access the someObject variable. So I'm wondering what's the rationale behind this method? Why should one use this way of casting rather than the (old) one - it only seems to move the problem of a failed cast "deeper" into the code.</p>