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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:21:07+00:00 2026-05-11T17:21:07+00:00

See the four lines in the Go() method below: delegate void Action<T>(T arg); delegate

  • 0

See the four lines in the Go() method below:

delegate void Action<T>(T arg);
delegate void Action();

void DoSomething<T>(Action<T> action)
{
    //...
}

void DoSomething(Action action)
{
    //...
}

void MyAction<T>(T arg)
{
    //...
}

void MyAction()
{
    //...
}

void Go<T>()
{
    DoSomething<T>(MyAction<T>); // throws compiler error - why?
    DoSomething(new Action<T>(MyAction<T>)); // no problems here
    DoSomething(MyAction); // what's the difference between this...
    DoSomething(new Action(MyAction)); // ... and this?
}

Note that the compiler error generated by the first call is:
The type arguments for method ‘Action(T)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

  • 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-11T17:21:07+00:00Added an answer on May 11, 2026 at 5:21 pm

    There’s no difference between MyAction and new Action(MyAction) (when they’re both valid) other than the former won’t work in C# 1. This is an implicit method group conversion. There are times that this isn’t applicable, most notable when the compiler can’t work out what kind of delegate you want, e.g.

    Delegate foo = new Action(MyAction); // Fine
    Delegate bar = MyAction; // Nope, can't tell target type
    

    This comes into play in your question because both of the methods involved are overloaded. This leads to headaches, basically.

    As for the generics side – it’s interesting. Method groups don’t get much love from C# 3 type inference – I’m not sure whether that’s going to be improved in C# 4 or not. If you call a generic method and specify the type argument, type inference works fairly well – but if you try to do it the other way round, it fails:

    using System;
    
    class Test
    {
        static void Main()
        {
            // Valid - it infers Foo<int>
            DoSomething<int>(Foo);
            // Valid - both are specified
            DoSomething<int>(Foo<int>);
            // Invalid - type inference fails
            DoSomething(Foo<int>);
            // Invalid - mismatched types, basically
            DoSomething<int>(Foo<string>);
        }
    
        static void Foo<T>(T input)
        {
        }
    
        static void DoSomething<T>(Action<T> action)
        {
            Console.WriteLine(typeof(T));
        }
    }
    

    Type inference in C# 3 is very complicated, and works well in most cases (in particular it’s great for LINQ) but fails in a few others. In an ideal world, it would become easier to understand and more powerful in future versions… we’ll see!

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

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can query the cache in a number of different… May 11, 2026 at 10:00 pm
  • Editorial Team
    Editorial Team added an answer You are only selecting columns from the table on the… May 11, 2026 at 10:00 pm
  • Editorial Team
    Editorial Team added an answer By the way, string interning is completely unrelated to that.… May 11, 2026 at 10:00 pm

Related Questions

I have a body of text that I have to scan and each line
Background: I'm using the SlimDX C# wrapper for DirectX, and am drawing many 2D
The code golf series seem to be fairly popular. I ran across some code
I'm trying to diagnose a problem in UKSyntaxColoredTextDocument 0.4 http://www.zathras.de/angelweb/blog-uksctd-oh-four.htm where text that actually
Ok, I have a set of checkboxes for selecting criteria. For argument's sake, we'll

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.