Quantcast
Channel: Matt Davey » C# Best Practices
Viewing all articles
Browse latest Browse all 6

Can access modifiers affect performance?

$
0
0

I saw an interesting question on StackOverflow yesterday, the poster asked the question “can making a method private improve performance?”. Of course the answer is a resounding no, but it got me thinking about how other access modifiers might affect performance. It’s no surprise that marking a method as “static” reduces call time significantly (in relation to an instance level method). Another access modifier that can help improve performance is “sealed”, since the CLR can now take a potentially big shortcut on the vtable when resolving the method call.

At the same time, the “virtual” access modifier can reduce performance, as the CLR has to query the vtable to resolve the method at runtime.

The big wildcards are “volatile” and the new “async” modifier in C#5. Both of those access modiers behave a little more like aspects, where the compiler generates extra code at compile time. These access modifiers also have a negative impact on performance, although not for the same reasons as “virtual”.



Viewing all articles
Browse latest Browse all 6

Trending Articles