Replies: 2 comments
-
Virtual methods and interface dispatch are quite similar, but they are separated. The effective |
Beta Was this translation helpful? Give feedback.
-
Yes it does.
The reason is the structure of internal CLR implementation. Virtual method info is direct in type info, and interface dispatch list is indirect. The difference is so small and bounded to implementation detail, that you shouldn't consider it. BTW please use BenchmarkDotNet to get reliable performance result. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I accidentally implemented interface in multiple level and I found something unexpected.
Circle shape inherits Shape, but implements IShape again explicitly. And when I call
Do
onCircle
, it callsIShape.Do
of circle and not of the base class (it does not havenew
keyword, also it doesn't complain that it should havenew
keyword) . It outputs 3 for Shape and 2 for Circle.I found this interesting as I can use it instead of virtual method. Not only this, I can explicitly override base method functionality even if it is not virtual provided caller uses only interface.
It turns out that explicit interface implementation is slightly slower, https://dotnetfiddle.net/EGEz1m
Beta Was this translation helpful? Give feedback.
All reactions