tags:

views:

117

answers:

2

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?

+1  A: 

There may be a minor difference, but it should be negligible unless the method is called repeatedly in a loop.

Partial methods are just syntactic sugar that allow the splitting of a class across multiple files.

When compiled, they are exactly the same as if all of the methods had been in the one file. In other words, the only 'slowdown' you are likely to see with a partial method is the slightly longer compile time :)

EDIT: And as the answer below says, they're not even in there if they can't be found during the compile.

Andrew Rollings
+3  A: 

If you implement the partial method, then I would expect there to be no noticeable difference. C# always uses callvirt to 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

Marc Gravell