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?
Because StringBuilder.Append calls ToString on the argument you provided. As far as this is a delegate casted as CD, it returns it's type.
To have the values A or X returned, the delegate has to be invoked. But Append does not expect a delegate and therefore it won't invoke it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With