Going a little further than the linked question in the comments about "local" variables...
A "local" variable is a variable which the lifetime is defined by the curly brackets in which it is contained. For example:
void SomeMethod()
{
int a = 0; //a is a local variable that is alive from this point down to }
}
But there are other types of local variables, for example:
void SomeMethod()
{
for (int i = 0; i < 10; i++)
{
int a = 0;
//a and i are local inside this for loop
}
//i and a do not exist here
}
Or even something like this is valid (but not recommended):
void SomeMethod()
{
int x = 0;
{
int a = 0;
//a exists inside here, until the next }
//x also exists in here because its defined in a parent scope
}
//a does not exist here, but x does
}
The {
and }
are scoping delimiters. They define a scope of something. When defined under a method, they define the scope for the code that belongs to the method. They also define the scope of things like for
, if
, switch
, class
, etc. They define a local scope.
For completeness, here is a class/member variable:
public class SomeClass
{
public int SomeVariable;
}
Here, SomeVariable
is defined in the SomeClass
scope, and can be accessed through instances of the SomeClass
class:
SomeClass sc = new SomeClass();
sc.SomeVariable = 10;
People call static
variables class variables but I don't agree with the definition, static classes are like singleton instances and I like to think of them as member variables.
Its also highly recommended that you use properties instead of public mutable members when exposing data outside a class.