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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:54:02+00:00 2026-06-14T06:54:02+00:00

I was reading Jon Skeet’s answer here and one of his samples was :

  • 0

I was reading Jon Skeet’s answer here

and one of his samples was :

  static void Main()
    {
        int x = 0;
        Foo( delegate { return x; } );
    }

    static void Foo(Func<int, int> action)
    {
        Console.WriteLine("I suspect the anonymous method...");
    }

But how Foo(Func<int, int>) can deal with delegate { return x; } which is Func<int> ?

As a matter of fact also Func< int,int,int,int,...> can deal with delegate { return x; } …

question 1
Any explanation for this behavior please ?

question 2

I have this code :

class MyClass
{
    public delegate void MyEventHandler(object sender);
    public  event MyEventHandler MyEvent;
}

and I wanted to use generic handlers so :

class MyClass
{
    public  Action<object> MyEventHandler;
    public  event MyEventHandler MyEvent;
}

but I get this error :

‘UserQuery.MyClass.MyEventHandler(object)’ is a ‘method’ but is used like a ‘type’

Why isn’t it recognizing it ?

  • 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-14T06:54:04+00:00Added an answer on June 14, 2026 at 6:54 am

    The answer to your first question is that the arguments to the delegate are inferred by the compiler. An anonymous method is compiled that takes an int as an argument, and does nothing at all with it. The code could have been written like this:

    Foo( delegate (int dummy) { return x; } );
    

    By omitting the parameter list, you are telling the compiler to emit a method that has the same argument types as the delegate type in the current context. The compiler knows that a Func<int, int> is expected, so it is able to make the anonymous method’s argument types match.

    In other words, delegate { ... } tells the compiler “create an anonymous method, but I don’t care what the arguments are because I’m not going to use them, so emit whatever arguments will allow this delegate to be acceptable in this context.” (This is different from delegate () { ... } which means “create an anonymous method that takes zero arguments.” Note the parentheses. If you add these parentheses to your example code, it will indeed fail to compile.)

    In the second case, you are using a field name where a type name is expected. Defining a field does not create a type with the field’s name. Your first example is correct; in that case, MyEventHandler is a type and not a field. (Even if that did work, using generics would not really be helpful, since the final delegate signature is already determined at compile time. You would get no net advantage from using generics.)

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

Sidebar

Related Questions

Im reading Jon Skeet book. (#4) but one thing (among others) caught my eye
When reading Jon Skeet 's answer to this particular question How can I make
I've started reading Jon Skeet's early access version of his book, which contains sections
I was reading Jon Skeet's refcard on C#. He stated: Events are closely related
Just finished reading Jon Skeet's article about events and delegates and got a question.
I am reading through Jon Skeet's C# in Depth, first edition (which is a
I'm reading the book Real-world functional programming by Tomas Petricek and Jon Skeet and
After becoming more engaged with training new engineers as well as reading Jon Skeet's
Im reading Jon Skeet book. ( Expression Trees Chapter) It has an example of
I've been reading Jon Skeet's C# In Depth: Second Edition and I noticed something

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.