I propose to have a look under the hood to know how the C# compiler deals with these two options.
Let's say I have this simplistic class:
class Test
{
private int _currentPosition;
public int OptionA()
{
ImplOutParameter(out _currentPosition);
return _currentPosition;
}
public int OptionB()
{
int currentPosition;
ImplOutParameter(out currentPosition);
return currentPosition;
}
public void ImplOutParameter(out int position)
{
position = 1;
}
}
The two first methods are very similar to yours. The third one is here to test the out
parameter implementation.
Let's compile this class in Release configuration. Using your favorite IL viewer, the method OptionA looks like:
It pushes the reference of the object on the stack twice to get the variable member reference and to call the ImplOutParameter method. Then it returns the value of the variable member.
The method OptionB looks like:
It pushes the reference of the object on the stack, pushes the address of the variable on the stack and call the ImplOutParameter method.Then it returns the value. It doesn't need to take time in order to allocate the variable.
Based on this analysis, I tend to say the OptionB implementation is a bit faster than the first one, but hey, this is very small. :)