Conditional operator doesn't work with two types that inherit the same base type

后端 未结 3 1771
遇见更好的自我
遇见更好的自我 2021-01-11 14:52

How come the conditional operator (?:) doesn\'t work when used with two types that inherit from a single base type?

The example I have is:



        
3条回答
  •  我在风中等你
    2021-01-11 15:32

    The conditional operator cannot determine the resultant type from its components, which may be either RedirectToRouteResult or RedirectResult. In order to resolve this, you should explicitly cast either (or both) of the components to the base type:

    ActionResult foo = (someCondition) ? 
                       (ActionResult)RedirectToAction("Foo","Bar") :
                       Redirect(someUrl);
    

提交回复
热议问题