Is there a difference from POV Performance / MemoryUsage initializing a object before, or after a return condition, like in the “sample”:
Function Foo() as ComplexObject
' is there a difference ??? '
' A '
' Dim obj as New ComplexObject() '
If condition Then Return Nothing
' is there a difference ??? '
' B '
Dim obj as New ComplexObject()
...
Return obj
End Function
If you mean, but comparison to:
before the
If condition Then Return Nothing, then yes: there is a difference: it done before, then it willnewan object each time, even if it is quickly discarded and collected from gen-0 (for the case whenNothingis returned). However, if you just declare it (withoutNew) before theIf, then the two should be identical (the position of the local variable is irrelevant, since all locals in IL are method-wide).I don’t know the VB, but in C# you could also use a conditional operator: