>>with endwith is really compiler sugar and the compiler behind the scenes actually creates a variable and uses it.
>>
>>So if you want to do this in C# the way to approach this is to use a local variable instead of the with/endwith which gives the same performance and most likely very close IL code.
>>
>
>Is there still one extra level of indirection ? If not then a simple VB
>
>WITH Me
> .Width = 1 'etc
>ENDWITH
>'would be identical to
>Dim o as Object = Me
> o.Width = 1 'etc
>
The best way to check this is to create that code and then look at it with IDLASM or Reflector and see what actually gets generated.
I haven't done this but my guess is that code will look very similar. I'd also compare the C# code for the latter just in case.
An optimizing compiler should be able to tell that a reference is being reused in the same scope and instead of walking the entire reference again, reuse a marker variable to hold the value. . syntax implies reference types so using a holder var is safe.
But as a rule of thumb in my code if a reference has more than two dots in it and I use it more than twice I will assign it to a local variable.
All of this for me is for ease of typing and readability. I don't think the performance implications are anything to worry much about unless you're running the code in a long, tight loop.