Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to distingish between local and static variables of same name

Tags:

scope

c#

static

example to illustrate :

public class Something
{
    private static int number;

    static Something()
    {
        int number = 10;

        // Syntax to distingish between local variable and static variable ?
    }
}

Inside the static constructor, is there a syntax that can be used to distinguish between the local variable called "number", and the static variable of the same name ?

like image 506
Moe Sisko Avatar asked Aug 26 '11 00:08

Moe Sisko


People also ask

Can I have same name for local and static variable?

Neither of the local static int sVar; will be allocated on the stack (if there is such a concept at all), each function body is a separate block, which limits the scope of the local static variable. The compiler uses a renaming scheme to ensure these variables are distinct at link time.

Can a variable be local and static at the same time?

In Java, a static variable is a class variable (for whole class). So if we have static local variable (a variable with scope limited to function), it violates the purpose of static. Hence compiler does not allow static local variable.

Can local variables have the same name?

Although it is usually a bad idea, you can declare a formal parameter or a local variable with the same name as one of the instance variables.

What are the differences between local variables and local static variables?

Example. Static variables have a lifetime that lasts until the end of the program. If they are local variables, then their value persists when execution leaves their scope.


1 Answers

Something.number

Obvious, no?.

like image 111
Mark H Avatar answered Oct 12 '22 23:10

Mark H