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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:30:14+00:00 2026-05-27T16:30:14+00:00

I just typed the following code: Expression<Func<ContentItem, bool>> expression = fileTypeGroupID.HasValue ? n =>

  • 0

I just typed the following code:

Expression<Func<ContentItem, bool>> expression = 
                fileTypeGroupID.HasValue ? n => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value : n => true;

Visual Studio is saying it can’t infer the type of n.

The code seems fine to me – it’s just using a ternary operator to assign one of two Expression literals to an Expression variable.

Is Visual Studio just not smart enough to infer the type of n inside a ternary operator, or have I made some kind of mistake?

  • 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-27T16:30:15+00:00Added an answer on May 27, 2026 at 4:30 pm

    This question is asked almost every day in some form.

    The conditional operator type analysis proceeds from inside to outside, not outside to inside. The conditional operator does not know to what type its results are being assigned and then coerces the consequence and alternative to those types. It does the opposite; it works out the types of the consequence and alternative, takes the more general of those two types, and then verifies that the general type may be assigned.

    The consequence and alternative contain no information about what the type of the lambda should be, and therefore the type of the conditional cannot be inferred. Therefore it cannot be verified that the assignment is correct.

    It is edifying to consider why the language was designed that way. Suppose you have overloads:

     void M(Func<string, int> f) {}
     void M(Func<double, double> f) {}
    

    and a call

    M( b ? n=>n.Foo() : n => n.Bar() );
    

    Describe how overload resolution determines which overload of M is chosen in a world where types are inferred from outside to inside.

    Now consider this one:

    M( b1 ? (b2 ? n=>n.Foo() : n => n.Bar() ) : (b3 ? n=>n.Blah() : n=>n.Abc()) );
    

    Getting harder isn’t it? Now imagine that Foo, Bar, Blah and Abc were themselves methods that took funcs, and also had arguments with conditional operators containing lambdas.

    We do not wish to make the type inference process so complex without a corresponding huge benefit, and there is no such huge benefit for the conditional operator.

    What you should do in your case is cast one or both of the consequence and alternative to the specific type.

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

Sidebar

Related Questions

Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref
I've just read some related questions that came up when I typed the subject,
I'm just about to write my first application in a duck typed language (Groovy).
Use case: I've just entered insert mode, and typed some text. Now I want
Note I'm running windows, the path just looks like it's linus because I typed
I just tested which type of code is executed by the WinForms Designer in
The following code does not compile: #include <iostream> class Foo { std::string s; public:
I have the following code in D import std.stdio; class Thing { // Fields
I believe the expression T() creates an rvalue (by the Standard). However, the following
Given the following code snippet: type MyIntf = interface ['{C6184693-663E-419F-B2DA-4DA1A0E33417}'] procedure Foo; end; InvisiblePropInterfaces

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.