title: ActionResult as ViewResult returns null.. but I can explicitly cast?
body: <p>I'm writing some unit tests and I have a scenario where, if a condition is true, a controller action should return a <code>HttpNotFoundResult</code>, otherwise it should return a <code>ViewResult</code> and have a specific Model inside of it.</p>
  
  <p>As one of the tests (testing the scenario where it should return a <code>ViewResult</code>), I execute the action and then try and cast the result to a <code>ViewResult</code>. However, when using <code>var result = myController.MyAction() as ViewResult</code> (where <code>result</code> is an <code>ActionResult</code>), <code>result</code> always evaluates to null... but when I do <code>var result = (ViewResult)myController.MyAction()</code>, the result is casted just fine.</p>
  
  <p>Why is this? Do I not understand the usage of <code>as</code> properly?</p>
  
  <p>Relevant code:</p>
  
  <pre><code>// My controller
  public class MyController
  {
    .. 
    public ActionResult MyAction(bool condition)
    {
        if(condition)
           return HttpNotFound()
        return View(new object());
    }
  }
  
  // My test
  public void MyTest()
  {
    ....
    var controller = new MyController();
    var result = controller.MyAction(false) as ViewResult;
    // result should be casted successfully by as, but it's not, instead it's unll
    // however, this works
    var result = (ViewResult) controller.MyAction(false);
    // why is this?
  }
  </code></pre>
  
  <p>EDIT: Full example with gist. Sorry, it seems that it doesnt' like the syntax highlighting.
  <a href="https://gist.github.com/DanPantry/dcd1d55651d220835899" rel="nofollow">https://gist.github.com/DanPantry/dcd1d55651d220835899</a></p>