Should a local variable be declared whenever we access a property?

say that I have a property of auto the Public the Property P of As an Integer , and every time I want to read this variable as a function of P, I declare a local variable as such: dim _p = P , then read _p instead of P .

I wonder if that makes sense? will this actually make things slower (which, of course, is not my intention)

Btw, if we change the question to Public Property P As Object , are there any changes in the answer?

+3
source share
7 answers

Me.P ( _p) , . , , , .

, , , , . Me.P , _p.

+4

, , P _p. , , P, _p. , - .

+3

, - , , . , . , getter - , , , , .

+3

, , , , - . . , , .

+2

- . . , .

#. , , , #:

public int MyNumber
{
    get
    {
        return GetValueFromDatabase();
    }
}

public void main()
{
    lblFirst.Text = MyNumber;
    lblSecond.Text = MyNumber;
    lblThird.Text = MyNumber;
}

, , :

public void main()
{
    int _myNumber = MyNumber;
    lblFirst.Text = _myNumber;
    lblSecond.Text = _myNumber;
    lblThird.Text = _myNumber;
}

, , - , , . , - .

+2

​​ , , , , , .

. , .

+2

. , " ", , .

, .

+2

All Articles