Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler oddity with delegate constructors

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.

like image 884
leppie Avatar asked Nov 10 '11 09:11

leppie


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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...

like image 134
Jon Skeet Avatar answered Oct 21 '22 09:10

Jon Skeet