3 ms·
> Doesn't this mean that either your methods won't be dynamically dispatched... The MSIL (.NET bytecode) for calling an extension method and a static method ar
by jdmichal 13y ago
> Doesn't this mean that either your methods won't be dynamically dispatched...
The MSIL (.NET bytecode) for calling an extension method and a static method are exactly the same. So yes, it will not be dynamically dispatched. Specifically, it emits a "call" and not a "callvirt" instruction.
However, the compiler will always prefer a class method over an extension method of the same signature. Meaning that extension methods can be specifically implemented by a class and the compiler will emit a "callvirt" instruction for that instance method instead.
> ... or that you would need to rewrite your code if you ever change from e. g. List to Array?
I'm not sure what you mean here. Arrays implement IList, so if you need random access, you would just write a method against IList. If you don't need random access, you should write it against IEnumerable, which all collections implement.
- jasomill 13y agoPerhaps what the parent is saying is that if you implement an extension method to IList, you can't subsequently implement an extension method to Array and expect it to be called on an Array stored in an IList variable? Which is true for exactly the reason you state.
- jdmichal 13y agoAh, yes. That would be true.