I have the following piece of code:
delegate string CD();
void MyFunction()
{
stringBuilder.Append((CD)delegate()
{
switch(whatever)
{
case 1 : return "A";
...
default: return "X";
}
});
}
But the stringBuilder appends text MyNamespace.MyClass+CD instead of A or X. Why does this happen?
You have declared a delegate of type
CDwithin your call toAppendandToString()is called on it which returns the type name by default i.e. “MyNamespace.MyClass+CD”.You need to invoke the delegate to get it to evaluate, e.g.: