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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:30:20+00:00 2026-05-10T23:30:20+00:00

I am wondering why the C# 3.0 compiler is unable to infer the type

  • 0

I am wondering why the C# 3.0 compiler is unable to infer the type of a method when it is passed as a parameter to a generic function when it can implicitly create a delegate for the same method.

Here is an example:

class Test {     static void foo(int x) { }     static void bar<T>(Action<T> f) { }      static void test()     {         Action<int> f = foo; // I can do this         bar(f); // and then do this         bar(foo); // but this does not work     }    } 

I would have thought that I would be able to pass foo to bar and have the compiler infer the type of Action<T> from the signature of the function being passed but this does not work. However I can create an Action<int> from foo without casting so is there a legitimate reason that the compiler could not also do the same thing via type inference?

  • 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. 2026-05-10T23:30:20+00:00Added an answer on May 10, 2026 at 11:30 pm

    Maybe this will make it clearer:

    public class SomeClass {     static void foo(int x) { }     static void foo(string s) { }     static void bar<T>(Action<T> f){}     static void barz(Action<int> f) { }     static void test()     {         Action<int> f = foo;         bar(f);         barz(foo);         bar(foo);         //these help the compiler to know which types to use         bar<int>(foo);         bar( (int i) => foo(i));     } } 

    foo is not an action – foo is a method group.

    • In the assignment statement, the compiler can tell clearly which foo you’re talking about, since the int type is specified.
    • In the barz(foo) statement, the compiler can tell which foo you’re talking about, since the int type is specified.
    • In the bar(foo) statement, it could be any foo with a single parameter – so the compiler gives up.

    Edit: I’ve added two (more) ways to help the compiler figure out the type (ie – how to skip the inference steps).

    From my reading of the article in JSkeet’s answer, the decision to not infer the type seems to be based on a mutual infering scenario, such as

      static void foo<T>(T x) { }   static void bar<T>(Action<T> f) { }   static void test()   {     bar(foo); //wut's T?   } 

    Since the general problem was unsolve-able, they choose to left specific problems where a solution exists as unsolved.

    As a consequence of this decision, you won’t be adding a overload for a method and getting a whole lot of type confusion from all the callers that are used to a single member method group. I guess that’s a good thing.

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

Sidebar

Related Questions

Im just wondering how good the MSVC++ Compiler can optimize code(with Code examples) or
I was wondering how I might be able to create a re-usable method that
I was wondering if the compiler (ECJ, javac, or you can name your favourite
I was wondering if there is some compiler parameter, preferably in gcc (g++) which
Just wondering why this does not compile. I thought the compiler could infer types
I am wondering that how Google's Closure Compiler works. Can I, for instance, make
In the following function, I'm wondering if the compiler is clever enough to work
I was wondering if there is any Borland cross compiler that can make my
I was wondering whether you could use a trick of the compiler to include
I was wondering if there is a possible optimization where the compiler does not

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.