Methods 1 and 2 are virtually the same. Method 1, obviously, declares the object in a separate expression for the purpose, but if two lines are next to each other in the code, then it is also actually similar to methods 2 and 3. In this case, I will always use method 3, since it is a required succin. As Darin says, they all generate the same IL.
I would use only method 1, when the declaration and purpose should have different scope, for example. the assignment is performed in the If block, and the value must be checked outside this block.
source share