ECMA-334 C# Language Specification10.7: Scopes |
The scope of a name is the region of program text within which it is possible to refer to the entity declared by the name without qualification of the name. Scopes can be nested, and an inner scope may redeclare the meaning of a name from an outer scope.
namespace-member-declaration (16.4) with no enclosing namespace-declaration is the entire program text. namespace-member-declaration within a namespace-declaration whose fully qualified name is N, is the namespace-body of every namespace-declaration whose fully qualified name is N or starts with N, followed by a period. using-directive (16.3) extends over the namespace-member-declarations of the compilation-unit or namespace-body in which the using-directive occurs. A using-directive may make zero or more namespace or type names available within a particular compilation-unit or namespace-body, but does not contribute any new members to the underlying declaration space. In other words, a using-directive is not transitive, but, rather, affects only the compilation-unit or namespace-body in which it occurs. class-member-declaration (17.2) is the class-body in which the declaration occurs. In addition, the scope of a class member extends to the class-body of those derived classes that are included in the accessibility domain (10.5.2) of the member. struct-member-declaration (18.2) is the struct-body in which the declaration occurs. enum-member-declaration (21.3) is the enum-body in which the declaration occurs. method-declaration (17.5) is the method-body of that method-declaration. indexer-declaration (17.8) is the accessor-declarations of that indexer-declaration. operator-declaration (17.9) is the block of that operator-declaration. constructor-declaration (17.10) is the constructor-initializer and block of that constructor-declaration. labeled-statement (15.4) is the block in which the declaration occurs. local-variable-declaration (15.5.1) is the block in which the declaration occurs. switch-block of a switch statement (15.7.2) is the switch-block. for-initializer of a for statement (15.8.3) is the for-initializer, the for-condition, the for-iterator, and the contained statement of the for statement. local-constant-declaration (15.5.2) is the block in which the declaration occurs. It is a compile-time error to refer to a local constant in a textual position that precedes its constant-declarator. Within the scope of a namespace, class, struct, or enumeration member it is possible to refer to the member in a textual position that precedes the declaration of the member.
class A
{
void F() {
i = 1;
}
int i = 0;
}
Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual position that precedes the local-variable-declarator of the local variable.
class A
{
int i = 0;
void F() {
i = 1; // Error, use precedes declaration
int i;
i = 2;
}
void G() {
int j = (j = 1); // Valid
}
void H() {
int a = 1, b = ++a; // Valid
}
}
local-variable-declarator. In the H method, a subsequent local-variable-declarator correctly refers to a local variable declared in an earlier local-variable-declarator within the same local-variable-declaration. end example]
the name A is used in an expression context to refer to the local variable A and in a type context to refer to the class A. end note]
using System;
class A {}
class Test
{
static void Main() {
string A = "hello, world";
string s = A; // expression context
Type t = typeof(A); // type context
Console.WriteLine(s); // writes "hello, world"
Console.WriteLine(t.ToString()); // writes "Type: A"
}
}
In This Section: