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 8069719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:09:14+00:00 2026-06-05T13:09:14+00:00

How come the conditional operator ( ?: ) doesn’t work when used with two

  • 0

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:

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

Where the long form works fine:

ActionResult foo;

if(someCondition)
{
  foo = RedirectToAction("Foo","Bar");
}
else
{
  foo = Redirect(someUrl);
}

Both return types, RedirectToRouteResult and RedirectResult, inherit from ActionResult.

  • 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-06-05T13:09:15+00:00Added an answer on June 5, 2026 at 1:09 pm

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

    The type of the conditional expression has to be either the type of the second operand or the type of the third operand, as per the language specification. The compiler doesn’t try to find a common base type, or another type that both operands can be converted to. The use of the expression doesn’t affect how its type is determined – so the variable assignment is irrelevant here.

    As for why the language is defined like this – it makes it considerably simpler to specify, implement, test and predict. This is fairly common in language design – keeping the language simple is usually a better bet in the long run, even if it makes it slightly more awkward in some specific situations.

    See section 7.14 of the C# 4 spec for more details.

    Casting either the second or third operand to the type that you actually want for the conditional expression is the way to fix the problem. Note that another situation this often comes up in is nullable types:

    // Invalid
    int? a = SomeCondition ? null : 10;
    
    // All valid
    int? a = SomeCondition ? (int?) null : 10;
    int? b = SomeCondition ? default(int?) : 10;
    int? c = SomeCondition ? null : (int?) 10;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've come across two different ways to define/name objects and functions in JavaScript that
In my career I've come across two broad types of theory: physical theories and
I come from a C++ background and I've been working with C# for about
I come from the asp.net world where we'd use an objectdatasource, hooked up to
I come from a php background and in php, there is an array_size() function
I`v come across a need where I want to create multiple list items from
How come every object appears to be marked new, instead of just objects that
I have the following code which is used to Push and Pend from a
So I have this stinky method, the two conditional blocks do almost the exact
I have a class that is created from an enum that can be tested

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.