views:

241

answers:

2

What's the performance consequence using the 'With' keyword in vb.net instead of using reusing the instance name over and over?

+1  A: 

There is no runtime performance cost. It is just "syntactic sugar" to make your code look prettier.

Eilon
+1  A: 

Assuming that you're comparing it to a local variable reference, there is no difference whatsoever; both will emit the exact same IL. (At least in Release mode)

However, if you're comparing it to repeated invocations of a property or indexer, With will be a little bit faster, and if you're comparing it to repeated invocations of a method, it might be much faster. (The With keyword will create a local variable and assign it to the object that you With'd, so the method will only be called once instead of on every line)

SLaks
+1. Although I'd say you should only worry about these performance differences if you've measured and found a serious performance bottleneck in a specific block of code that uses With. "Premature micro-optimization is the root of all evil"
MarkJ