I’m updating my code generator and have a choice between implementing a method stub as a Virtual Method in a base class or a partial method in the generated code. Is there any performance difference between the two?
I’m updating my code generator and have a choice between implementing a method stub
Share
If you implement the partial method, then I would expect there to be no noticeable difference. C# always uses
callvirtto invoke instance methods, even if they aren’t virtual, so there won’t be much change.If you don’t implement the partial method, then the call itself is removed – so there is never a stack to prepare etc. This will be infintessimally quicker, which is why it is fine for generated code to include a ludicrous number of partial method stubs: if you don’t use them, they don’t exist.
The bigger reason to use partial methods is simply so you don’t have to subclass the object. You can’t declare ‘abstract’/’virtual’ and ‘override’ parts of a virtual method in the same class (even if partial). Partial methods solves this problem, and the problem of advertising extensibility points (without having to use reflection).
Very good for code-generation tools ;-p