←to practical programming

Note "scope"

In C# variables are lexically scoped: the scope of the variable can be determined at compile time and is independent of the state of the runtime-system. Scope-wise the variables in C# can be divided into three classes,

Variables declared in the class-scope (also called fields or members) can be accessed anywhere in the class and can also be accessed outside of the class (if decleared public).

Variables declared inside a method are accessible anywhere inside the method but are not accessible outside of the method. Method-scope variables can shadow class-scope variables. Method-scope variables are deleted when the method exits.

Variables declared inside a block are block-scope variables. They are not visible outside their block. Block-scope variables cannot shadow class- and method-scope variables. Block-scope variables are deleted when the block exits.