Extension methods defined on value types cannot be used to create delegates – Why not?

In response to my other answer, Eric Smith correctly notes: “… because it would require implicitly boxing the receiver type parameter …”. Which is what happens anyway, if you do something like this: Func f = 5.ToString; Which is perfectly legal. Thinking about this has led me to a new answer. Try this on for … Read more

Does the ‘readonly’ modifier create a hidden copy of a field?

Does the readonly modifier create a hidden copy of a field? Calling a method or property on a read-only field of a regular struct type (outside the constructor or static constructor) first copies the field, yes. That’s because the compiler doesn’t know whether the property or method access would modify the value you call it … Read more

Why are delegates reference types?

The question boils down to this: the CLI (Common Language Infrastructure) specification says that delegates are reference types. Why is this so? One reason is clearly visible in the .NET Framework today. In the original design, there were two kinds of delegates: normal delegates and “multicast” delegates, which could have more than one target in … Read more

Does swift copy on write for all structs?

Array is implemented with copy-on-write behaviour – you’ll get it regardless of any compiler optimisations (although of course, optimisations can decrease the number of cases where a copy needs to happen). At a basic level, Array is just a structure that holds a reference to a heap-allocated buffer containing the elements – therefore multiple Array … Read more