I tried disassembling a C# created executable, but I couldn't come to a conclusion. What I'd like to know is that if for the CLR c#'s delegates are really special entities or just a compiler sugar?
I ask this because I'm implementing a language that compiles to C#, and it would be much more interesting for me to compile anonymous functions as classes than as delegates. But I don't want to use a design that later I will regret, as they might be heavier on memory (I think of Java's PermGen to base my questioning on. Even though I know there is no such thing for the CLR).
Thank you!
--edit
to be a little more clear, I'd like to know if there is (and what are) the differences between:
void Main()
{
Func<int, int, int> add = delegate(int a, int b) {return a + b;};
}
and, for example
class AnonFuncion__1219023 : Fun3
{
public override int invoke(int a, int b)
{
return a + b;
}
}
-- edit
I think that there might be a great difference between:
class Program
{
static int add(int a, int b)
{
return a + b;
}
static void Main()
{
Func<int, int, int> add = Program.add;
}
}
and
class Function__432892 : Fun3
{
public override int invoke(int a, int b)
{
return Program.add(a, b);
}
}
I read somewhere, though, that the syntax Func<int, int, int> add = Program.add;
is only a sugar for Func<int, int, int> add = delegate(int a, int b) { return Program.add; };
. But I really don't know if that's really true.
I could also see that the C# compiler already caches all those instances so they are constructed only once. I could do the same with my compiler, though.