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

  • Home
  • SEARCH
  • 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 6072229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:08:12+00:00 2026-05-23T10:08:12+00:00

If var keyword is resolved at compile time, how does the following work? class

  • 0

If var keyword is resolved at compile time, how does the following work?

class A {
}
class B : A {
}

int k = 1;
var x = (k < 0) ? new B() : new A();

Edit:
I finally understood that the problem is not about the var itself, but about the behaviour of the ?: operator. For some reason, I thought that the following could be possible:

object x = something ? 1 : ""

and that’s not possible at all 🙂

Related question (about ternary operator):
Why assigning null in ternary operator fails: no implicit conversion between null and int?

  • 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-05-23T10:08:13+00:00Added an answer on May 23, 2026 at 10:08 am

    The result is of type A, because both of the variables are of type A, and at least one of them is directly of type A (not through some conversion).

    The compiler takes a look at both parts of the ternary expression, and if one of them is a subtype of the other, the entire expression becomes the more general supertype.

    However, if neither is directly of the common type, then a compiler error occurs, probably because it doesn’t know how much to upcast for you (and it doesn’t feel like finding out).

    See here:

    The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

    condition ? first_expression : second_expression;
    

    […]

    Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var keyword gets the type at the runtime or compile time? or depends?
Possible Duplicate: C# 'var' keyword versus explicitly defined variables EDIT: For those who are
I like the fact that ReSharper suggest me to use the var keyword. Instead,
Possible Duplicate: Use of var keyword in C# Being relatively new to C# I
I wanted to use the var keyword to declare a field in my class
I'm quite new to LINQ & Entity framework as well as the var keyword
How C# compiler interpret objects that specified by var keyword? When do we must
Possible Duplicate: Use of var keyword in C# Hi, I am pretty new in
C# 3.0 introduced the var keyword. And when compiled, the compiler will insert the
After discussion with colleagues regarding the use of the 'var' keyword in C# 3

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.