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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:20:04+00:00 2026-05-13T13:20:04+00:00

Suppose I have public static List<T2> Map<T,T2>(List<T> inputs, Func<T, T2> f) { return inputs.ConvertAll((x)

  • 0

Suppose I have

public static List<T2> Map<T,T2>(List<T> inputs, Func<T, T2> f)
{
    return inputs.ConvertAll((x) => f(x));
}

private int Square(int x) { return x*x; }

public void Run()
{
    var inputs = new List<Int32>(new int[]{2,4,8,16,32,64,128,256,512,1024,2048});

    // this does not compile
    var outputs = Map(inputs, Square); 

    // this is fine
    var outputs2 = Map<Int32,Int32>(inputs, Square);

    // this is also fine (thanks, Jason)
    var outputs2 = Map<Int32,Int32>(inputs, (x)=>x*x);

    // also fine
    var outputs2 = Map(inputs, (x)=>x*x);
}

Why does it not compile?

EDIT: The error is:

error CS0411: The type arguments for method ‘Namespace.Map<T,T2>(System.Collections.Generic.List<T>, System.Func<T,T2>)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

Why do I have to specify the type of the Map() function? Can it not infer this from the passed Func<T> ? (in my case, Square)


Is the answer the same as for
C# 3.0 generic type inference – passing a delegate as a function parameter ?

  • 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-13T13:20:04+00:00Added an answer on May 13, 2026 at 1:20 pm

    From your error message:

    The type arguments for method ‘[...].Map<T,T2>(System.Collections.Generic.List<T>, System.Func<T,T2>)‘ cannot be inferred from the usage. Try specifying the type arguments explicitly.

    Note that the error message says that it can not figure out the type arguments. That is, it is having trouble resolving one of the type parameters T or T2. This is because of §25.6.4 (Inference of type arguments) of the specification. This is the part of the specification the deals with inferring generic type parameters.

    Nothing is inferred from the argument (but type inference succeeds) if any of the following are true:

    […]

    The argument is a method group.

    Thus, the compiler is not able to use the delegate type of Square to infer the type of T2. Note that if you change the declaration to

    public static List<T> Map<T>(List<T> inputs, Func<T, T> f) {
            return inputs.ConvertAll((x) => f(x));
    }
    

    then

    var outputs = Map(inputs, Square);
    

    is legal. In this case, it has already resolved that T is int from the fact that inputs is a List<int>.

    Now, the deeper question is why is the above the specification? That is, why don’t method groups play a role in type parameter resolution? I think it’s because of cases like this:

    class Program {
        public static T M<T>(Func<T, T> f) {
            return default(T);
        }
    
        public static int F(int i) {
            return i;
        }
    
        public static float F(float f) {
            return f;
        }
    
        static void Main(string[] args) {
            M(F); // which F am I?
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this class: public class DispatcherService<T> { private static Action<T> Dispatcher; public
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have a method public Patient(int id) { ---- } that returns Patient
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have a method to return a list of keys in a dictionary,
Suppose I have a class like the following: public class Stage { public int
Suppose you have the following class: class Test : ISerializable { public static Test
Suppose I have this code (pseudocode) class SomeClass { class Person { public static
Suppose I have the following situation: 9 class Program 10 { 11 public static
Suppose I have BaseClass with public methods A and B, and I create DerivedClass

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.