C# Copying instance variable to local variable in functions of same class

Posted by NickLarsen on Stack Overflow See other posts from Stack Overflow or by NickLarsen
Published on 2010-04-15T13:25:15Z Indexed on 2010/04/15 13:33 UTC
Read the original article Hit count: 311

Filed under:
|

I have been looking through some code on an open source project recently and found many occurrences of this kind of code:

class SomeClass
{
    private int SomeNumber = 42;

    public ReturnValue UseSomeNumber(...)
    {
        int someNumberCopy = this.SomeNumber;
        if (someNumberCopy > ...)
        {
            // ... do some work with someNumberCopy
        }
        else
        {
            // ... do something else with someNumberCopy
        }
    }
}

Is there any real benefit to making a copy of the instance variable?

© Stack Overflow or respective owner

Related posts about c#

Related posts about instance-variables