开发者

VB.NET 'With' statement performance?

开发者 https://www.devze.com 2022-12-16 09:09 出处:网络
What\'s the performance consequence using the \'With\' keyword i开发者_运维技巧n vb.net instead of using reusing the instance name over and over?Assuming that you\'re comparing it to a local variable

What's the performance consequence using the 'With' keyword i开发者_运维技巧n vb.net instead of using reusing the instance name over and over?


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)


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消