What's the performance consequence using the 'With' keyword in vb.net instead of using reusing the instance name over and over?
3 Answers
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)
3 Comments
With with a method. Cool!There is no runtime performance cost. It is just "syntactic sugar" to make your code look prettier.
1 Comment
sub xyz (ByRef param as MyObj)
'Local ref, same as with
dim o2 as YourObject = param.YourObject
o2.callSomething()
'Bad performance
param.YourObject.callSomething()
end sub