G
Guest
Is there any difference between declaring a variable at the top of a method
versus in the code of the method? Is there a performance impact for either
choice? What about if the method will return before the variable is used?
I'm trying to get an idea of whether the .NET compilers for VB.NET and C#
will move all variable declaration to the beginning of a method or whether
they will allocate the memory as it is needed by the method to conserve
memory.
Example (VB.NET):
' Declaration at the top of a method.
Public Sub SampleSub()
Dim sMessage As String
' ... Other code that might exit the method early ...
sMessage = "Hello!"
End Sub
' Declaration in a method.
Public Sub SampleSub()
' ... Code that might exit the method early ...
Dim sMessage As String = "Hello!"
End Sub
versus in the code of the method? Is there a performance impact for either
choice? What about if the method will return before the variable is used?
I'm trying to get an idea of whether the .NET compilers for VB.NET and C#
will move all variable declaration to the beginning of a method or whether
they will allocate the memory as it is needed by the method to conserve
memory.
Example (VB.NET):
' Declaration at the top of a method.
Public Sub SampleSub()
Dim sMessage As String
' ... Other code that might exit the method early ...
sMessage = "Hello!"
End Sub
' Declaration in a method.
Public Sub SampleSub()
' ... Code that might exit the method early ...
Dim sMessage As String = "Hello!"
End Sub