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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:45:29+00:00 2026-05-26T17:45:29+00:00

Based on the following question , I found some odd behaviour of the c#

  • 0

Based on the following question, I found some odd behaviour of the c# compiler.

The following is valid C#:

static void K() {}

static void Main()
{
  var k = new Action(new Action(new Action(K))));
}

What I do find strange is the compiler ‘deconstructing’ the passed delegate.

The ILSpy output is as follows:

new Action(new Action(new Action(null, ldftn(K)), ldftn(Invoke)).Invoke);

As one can see, it automatically decides to use the Invoke method of the delegate. But why?

As it is, the code is unclear. Do we have a triply-wrapped delegate (actual) or is the inner delegate just ‘copied’ to the outer ones (my initial thought).

Surely if the intent was like the compiler emitted the code, one should have written:

var k = new Action(new Action(new Action(K).Invoke).Invoke);

Similar to the decompiled code.

Can anyone justify the reason for this ‘surprising’ transformation?

Update:

I can only think of one possible use-case for this; delegate type conversion. Eg:

delegate void Baz();
delegate void Bar();
...
var k = new Baz(new Bar( new Action (K)));

Perhaps the compiler should emit a warning if the same delegate types are used.

  • 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-26T17:45:30+00:00Added an answer on May 26, 2026 at 5:45 pm

    The spec (section 7.6.10.5) says:

    • The new delegate instance is initialized with the same invocation list as the delegate instance given by E.

    Now suppose the compiler translated it to something similar to your suggestion of:

    new Action( a.Target, a.Method)
    

    That would only ever create a delegate with an invocation list of a single method call. For a multi-cast delegate, it would violate the spec.

    Sample code:

    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            Action first = () => Console.WriteLine("First");
            Action second = () => Console.WriteLine("Second");
    
            Action both = first + second;
            Action wrapped1 =
                (Action) Delegate.CreateDelegate(typeof(Action),
                                                 both.Target, both.Method);
            Action wrapped2 = new Action(both);
    
            Console.WriteLine("Calling wrapped1:");
            wrapped1();
    
            Console.WriteLine("Calling wrapped2:");
            wrapped2();
        }
    }
    

    Output:

    Calling wrapped1:
    Second
    Calling wrapped2:
    First
    Second
    

    As you can see, the real behaviour of the compiler matches the spec – your suggested behaviour doesn’t.

    This is partly due to the somewhat odd “sometimes single-cast, sometimes multi-cast” nature of Delegate, of course…

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

Sidebar

Related Questions

Based on the following question: Check if one string is a rotation of other
For the following question, I am looking for an answer that is based on
My question is concerning SQL connection status, load, etc. based on the following code:
Now following my series of python newbie questions and based on another question .
I have the following requirement: Based on some user input, I need to generate
Suppose that one has some lock based code like the following where mutexes are
The samples and documentation in question can be found here: http://static.springsource.org/spring-flex/docs/1.5.0.M1/ The reference doc
Merging huge CSS files is huge pain, Hi i found some question asked on
This question is based on a previous similar question. I have the following equation
Based on the following example URL structure: mysite.com/mypage.aspx?a=red&b=green&c=blue Pages in the application use ASP.net

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.