Scope

Scope determines the region of program text (that is, lexical scoping) in which it’s valid to refer to a name of some entity without requiring further qualification. Scopes can be nested, too. For example, when declaring a class, a scope is introduced for its members. Some kinds of members, like methods, introduce a new scope:

class ScopeSample{    private int x = 42;    public void InnerScope()    {        int x = 53;        int y = x; // refers to the local variable x        // ...    }    public void OuterScope()    {        int y = x; // refers to the field x        // ...    }}

The InnerScope method in the preceding sample declares a local variable called x, hiding the class’s instance field that’s also called x. It’s actually possible ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.