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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:30:08+00:00 2026-06-17T09:30:08+00:00

I was reading C# in depth , got confused in 2-phase type inference rule

  • 0

I was reading C# in depth , got confused in 2-phase type inference rule explained by dear author.

Consider the code :

static void PrintConvertedValue<TInput,TOutput> (TInput input, Converter<TInput,TOutput> converter) 
{ Console.WriteLine(converter(input)); } 
... 
PrintConvertedValue("I'm a string", x => x.Length);

after which he explains the process of inference using 2-phase inference algo.

1.Phase 1 begins.

2.The first parameter is of type TInput, and the first argument is of type string. We infer that there must be an implicit conversion from
string to TInput.

3.The second parameter is of type Converter, and the second argument is an implicitly typed lambda expression. No inference
is performed—we don’t have enough information.

4.Phase 2 begins.

5.TInput doesn’t depend on any unfixed type parameters, so it’s fixed to string.

6.The second argument now has a fixed input type, but an unfixed output type. We can consider it to be (string x) => x.Length and infer
the return type as int. Therefore an implicit conversion must take
place from int to TOutput.

7.Phase 2 repeats.

8.TOutput doesn’t depend on anything unfixed, so it’s fixed to int. 9There are now no unfixed type parameters, so inference succeeds

I am very confused with step 2 and step 5. In step 2 how can compiler make inference like that ? I mean how in world conversion comes in to the scene? Conversion happens BTW types not BTW type parameters and types, isn’t it?.

And step 5 completely eluded me, can anyone present me simplified example where a type parameter can depend on other type parameter please with explanation what author is trying to imply?

I dared to lurk in to C# specification but seems intelligent peoples made it for very similar intelligent peoples, not for monkeys like me 🙂

  • 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-17T09:30:09+00:00Added an answer on June 17, 2026 at 9:30 am

    Conversion happens for values, and the fact that we’re able to call the method using a string value as the argument for a parameter of type TInput means that there must be a conversion from string to TInput. If TInput were inferred to be int, then we’d be calling a method requiring an int, but giving it a string – which is clearly not valid.

    Note that for a simpler example, just change it to:

    void Foo<T>(T input) { }
    

    called with:

    Foo("Hello");
    

    There has to be a conversion from string to T in order for the call to be valid, and as there’s no other information about T, we infer that T is simply string.

    In a slightly more complicated example, you could have:

    public void Bar<T>(T x, T y) {}
    
    Foo("Hello", new object());
    

    Here, we infer that:

    • There must be a conversion from string to T
    • There must be a conversion from object to T

    … so we infer that T is object.

    As for type parameter dependencies, from section 7.5.2.5 of the C# 5 specification:

    An unfixed type variable Xi depends directly on an unfixed type variable Xj if for some argument Ek with type Tk Xj occurs in an input type of Ek with type Tk and Xi occurs in an output type of Ek with type Tk.

    Xj depends on Xi if Xj depends directly on Xi or if Xi depends directly on Xk and Xk depends on Xj. Thus “depends on” is the transitive but not reflexive closure of “depends directly on”.

    So for example:

    public void Foo<T1, T2>(T1 value, Func<T1, T2> func)
    

    And:

    Foo("foo", x => x.Length);
    

    Here T2 depends on T1 – once we’ve inferred T1, we can infer T2.

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

Sidebar

Related Questions

I have been studying DDD in-depth on my own time; reading everything about it
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading some questions here on SO about conversion operators and constructors got me thinking
I am reading through Jon Skeet's C# in Depth, first edition (which is a
I was reading C# in depth and I couldn't understand the para of the
I started reading C# in depth. Now I'm in the journey of Generics. I
I am reading this book to study the concepts of CUDA in depth. In
I was reading this and unfortunately could not understand in depth why the compiler
i was reading the book Delphi Developer's Guide to OpenGL and this code should
I've got 2 questions. One's a specific question about why my code isn't working

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.