What is different between scope variable and public variable of a variable in C# You can declare variables at four different locations in your programs What is meant by the "scope" of a variable? think of the scope of a variable as "The scope of a particular variable is the range within a program's source code in which that variable is recognized by the compiler".
-
@rene: Not sure how this can be a duplicate of said question, it's asking something quite different, right? – O. R. Mapper Feb 09 '13 at 12:09
-
2Your quote is what explains "scope" so what is it exactly that you do not understand? What would a "public variable of a variable" be? – PhoenixReborn Feb 09 '13 at 12:09
4 Answers
If my reading of your question is correct, you are looking to contrast the scope (local, instance, static, etc.) with visibility (public, private, protected, internal). These two concepts are nearly independent (I said "nearly", because variables of local scope do not have visibility). Together they let you control both the lifetime and the accessibility of your variables.
The scope controls the lifetime of your variable, letting you determine when it comes into existence and when it becomes unavailable. Naturally, the scope constraints "the range within a program's source code in which that variable is recognized by the compiler". However, being recognized by the compiler is not sufficient for your program to access the variable: if the compiler recognizes the variable as private, it wouldn't let your program access it outside the context where the variable is visible (e.g. from methods of another class).
Essentially, scope and visibility control two different aspects of accessibility that work together to decide the range within a program's source code in which that variable can be used by the code that tries to access it.

- 714,442
- 84
- 1,110
- 1,523
The scope of the variable is how long you can use that name and be talking about the same thing.
If it's in a function, then look at the { }
s. Whenever you go into a { }
you're going into a deeper scope - variables declared inside that { }
can't be referenced from outside of it, but variables declared outside of it can be referenced from inside of it. In fact, you don't need an if, for or so on - you can declare a new { }
whenever you like in C#, so for example you could do something like this:
{
int a = foo();
int b = bar();
}
{
int a = foo();
int b = bar();
}
and the redeclaration is valid.
'Scope' for classes is actually something completely different called visibility. public
means if you are in this file or using
this file you can reference it. protected
means you can only access it if you are an instance of that class (or its subclasses). private
means you can only access it if you are an instance of that class (not its subclasses). internal
is like public, except you also have to be in the same assembly - so if you compile as a .dll, then you can't reference internal variables from elsewhere. However, whether it's public/private/internal/protected doesn't control how long the variable lives for - its scope.

- 21,443
- 3
- 45
- 53
Something like that can help you: C# Variable scopes
Basically, there are 3 scopes: class-level, method-level, nested-level. Not to be confused with Access modifiers: public, internal, protected, private.

- 164
- 8
If a local variable is declared with the Static keyword, its lifetime is longer than the execution time of the procedure in which it is declared. If the procedure is inside a module, the static variable survives as long as your application continues running.

- 704
- 5
- 17
-
1-1. Local variables and static fields are not the same. And C# does not have procedures or modules. – dtb Feb 09 '13 at 12:37