10.3Class members
The members of a class consist of the members introduced by its class-member-declarations and the members inherited from the direct base class.
class-member-declarations:
class-member-declaration
class-member-declarations class-member-declaration
class-member-declaration:
constant-declaration
field-declaration
method-declaration
property-declaration
event-declaration
indexer-declaration
operator-declaration
constructor-declaration
destructor-declaration
static-constructor-declaration
type-declaration
The members of a class type are divided into the following categories:
-
Constants, which represent constant values associated with the class (§10.4).
-
Fields, which are the variables of the class (§10.5).
-
Methods, which implement the computations and actions that can be performed by the class (§10.6).
-
Properties, which define named characteristics and the actions associated with reading and writing those characteristics (§10.7).
-
Events, which define notifications that can be generated by the class (§10.8).
-
Indexers, which permit instances of the class to be indexed in the same way (syntactically) as arrays (§10.9).
-
Operators, which define the expression operators that can be applied to instances of the class (§10.10).
-
Instance constructors, which implement the actions required to initialize instances of the class (§10.11)
-
Destructors, which implement the actions to be performed before instances of the class are permanently discarded (§10.13).
-
Static constructors, which implement the actions required to initialize the class itself (§10.12).
-
Types, which represent the types that are local to the class (§10.3.8).
Members that can contain executable code are collectively known as the function members of the class type. The function members of a class type are the methods, properties, events, indexers, operators, instance constructors, destructors, and static constructors of that class type.
A class-declaration creates a new declaration space (§3.3), and the class-member-declarations immediately contained by the class-declaration introduce new members into this declaration space. The following rules apply to class-member-declarations:
-
Instance constructors, destructors and static constructors must have the same name as the immediately enclosing class. All other members must have names that differ from the name of the immediately enclosing class.
-
The name of a constant, field, property, event, or type must differ from the names of all other members declared in the same class.
-
The name of a method must differ from the names of all other non-methods declared in the same class. In addition, the signature (§3.6) of a method must differ from the signatures of all other methods declared in the same class, and two methods declared in the same class may not have signatures that differ solely by ref and out.
-
The signature of an instance constructor must differ from the signatures of all other instance constructors declared in the same class, and two constructors declared in the same class may not have signatures that differ solely by ref and out.
-
The signature of an indexer must differ from the signatures of all other indexers declared in the same class.
-
The signature of an operator must differ from the signatures of all other operators declared in the same class.
The inherited members of a class type (§10.3.3) are not part of the declaration space of a class. Thus, a derived class is allowed to declare a member with the same name or signature as an inherited member (which in effect hides the inherited member).
10.3.1The instance type
Each class declaration has an associated bound type (§4.4.3), the instance type. For a generic class declaration, the instance type is formed by creating a constructed type (§4.4) from the type declaration, with each of the supplied type arguments being the corresponding type parameter. Since the instance type uses the type parameters, it can only be used where the type parameters are in scope; that is, inside the class declaration. The instance type is the type of this for code written inside the class declaration. For non-generic classes, the instance type is simply the declared class. The following shows several class declarations along with their instance types:
class A // instance type: A
{
class B {} // instance type: A.B
class C {} // instance type: A.C
}
class D {} // instance type: D
10.3.2Members of constructed types
The non-inherited members of a constructed type are obtained by substituting, for each type-parameter in the member declaration, the corresponding type-argument of the constructed type. The substitution process is based on the semantic meaning of type declarations, and is not simply textual substitution.
For example, given the generic class declaration
class Gen
{
public T[,] a;
public void G(int i, T t, Gen gt) {...}
public U Prop { get {...} set {...} }
public int H(double d) {...}
}
the constructed type Gen> has the following members:
public int[,][] a;
public void G(int i, int[] t, Gen,int[]> gt) {...}
public IComparable Prop { get {...} set {...} }
public int H(double d) {...}
The type of the member a in the generic class declaration Gen is “two-dimensional array of T”, so the type of the member a in the constructed type above is “two-dimensional array of one-dimensional array of int”, or int[,][].
Within instance function members, the type of this is the instance type (§10.3.1) of the containing declaration.
All members of a generic class can use type parameters from any enclosing class, either directly or as part of a constructed type. When a particular closed constructed type (§4.4.2) is used at run-time, each use of a type parameter is replaced with the actual type argument supplied to the constructed type. For example:
class C
{
public V f1;
public C f2 = null;
public C(V x) {
this.f1 = x;
this.f2 = this;
}
}
class Application
{
static void Main() {
C x1 = new C(1);
Console.WriteLine(x1.f1); // Prints 1
C x2 = new C(3.1415);
Console.WriteLine(x2.f1); // Prints 3.1415
}
}
10.3.3Inheritance
A class inherits the members of its direct base class type. Inheritance means that a class implicitly contains all members of its direct base class type, except for the instance constructors, destructors and static constructors of the base class. Some important aspects of inheritance are:
-
Inheritance is transitive. If C is derived from B, and B is derived from A, then C inherits the members declared in B as well as the members declared in A.
-
A derived class extends its direct base class. A derived class can add new members to those it inherits, but it cannot remove the definition of an inherited member.
-
Instance constructors, destructors, and static constructors are not inherited, but all other members are, regardless of their declared accessibility (§3.5). However, depending on their declared accessibility, inherited members might not be accessible in a derived class.
-
A derived class can hide (§3.7.1.2) inherited members by declaring new members with the same name or signature. Note however that hiding an inherited member does not remove that member—it merely makes that member inaccessible directly through the derived class.
-
An instance of a class contains a set of all instance fields declared in the class and its base classes, and an implicit conversion (§6.1.6) exists from a derived class type to any of its base class types. Thus, a reference to an instance of some derived class can be treated as a reference to an instance of any of its base classes.
-
A class can declare virtual methods, properties, and indexers, and derived classes can override the implementation of these function members. This enables classes to exhibit polymorphic behavior wherein the actions performed by a function member invocation varies depending on the run-time type of the instance through which that function member is invoked.
The inherited member of a constructed class type are the members of the immediate base class type (§10.1.4.1), which is found by substituting the type arguments of the constructed type for each occurrence of the corresponding type parameters in the base-class-specification. These members, in turn, are transformed by substituting, for each type-parameter in the member declaration, the corresponding type-argument of the base-class-specification.
class B
{
public U F(long index) {...}
}
class D: B
{
public T G(string s) {...}
}
In the above example, the constructed type D has a non-inherited member public int G(string s) obtained by substituting the type argument int for the type parameter T. D also has an inherited member from the class declaration B. This inherited member is determined by first determining the base class type B of D by substituting int for T in the base class specification B. Then, as a type argument to B, int[] is substituted for U in public U F(long index), yielding the inherited member public int[] F(long index).
10.3.4The new modifier
A class-member-declaration is permitted to declare a member with the same name or signature as an inherited member. When this occurs, the derived class member is said to hide the base class member. Hiding an inherited member is not considered an error, but it does cause the compiler to issue a warning. To suppress the warning, the declaration of the derived class member can include a new modifier to indicate that the derived member is intended to hide the base member. This topic is discussed further in §3.7.1.2.
If a new modifier is included in a declaration that doesn’t hide an inherited member, a warning to that effect is issued. This warning is suppressed by removing the new modifier.
10.3.5Access modifiers
A class-member-declaration can have any one of the five possible kinds of declared accessibility (§3.5.1): public, protected internal, protected, internal, or private. Except for the protected internal combination, it is a compile-time error to specify more than one access modifier. When a class-member-declaration does not include any access modifiers, private is assumed.
10.3.6Constituent types
Types that are used in the declaration of a member are called the constituent types of that member. Possible constituent types are the type of a constant, field, property, event, or indexer, the return type of a method or operator, and the parameter types of a method, indexer, operator, or instance constructor. The constituent types of a member must be at least as accessible as that member itself (§3.5.4).
10.3.7Static and instance members
Members of a class are either static members or instance members. Generally speaking, it is useful to think of static members as belonging to class types and instance members as belonging to objects (instances of class types).
When a field, method, property, event, operator, or constructor declaration includes a static modifier, it declares a static member. In addition, a constant or type declaration implicitly declares a static member. Static members have the following characteristics:
-
When a static member M is referenced in a member-access (§7.5.4) of the form E.M, E must denote a type containing M. It is a compile-time error for E to denote an instance.
-
A static field identifies exactly one storage location to be shared by all instances of a given closed class type. No matter how many instances of a given closed class type are created, there is only ever one copy of a static field.
-
A static function member (method, property, event, operator, or constructor) does not operate on a specific instance, and it is a compile-time error to refer to this in such a function member.
When a field, method, property, event, indexer, constructor, or destructor declaration does not include a static modifier, it declares an instance member. (An instance member is sometimes called a non-static member.) Instance members have the following characteristics:
-
When an instance member M is referenced in a member-access (§7.5.4) of the form E.M, E must denote an instance of a type containing M. It is a compile-time error for E to denote a type.
-
Every instance of a class contains a separate set of all instance fields of the class.
-
An instance function member (method, property, indexer, instance constructor, or destructor) operates on a given instance of the class, and this instance can be accessed as this (§7.5.7).
The following example illustrates the rules for accessing static and instance members:
class Test
{
int x;
static int y;
void F() {
x = 1; // Ok, same as this.x = 1
y = 1; // Ok, same as Test.y = 1
}
static void G() {
x = 1; // Error, cannot access this.x
y = 1; // Ok, same as Test.y = 1
}
static void Main() {
Test t = new Test();
t.x = 1; // Ok
t.y = 1; // Error, cannot access static member through instance
Test.x = 1; // Error, cannot access instance member through type
Test.y = 1; // Ok
}
}
The F method shows that in an instance function member, a simple-name (§7.5.2) can be used to access both instance members and static members. The G method shows that in a static function member, it is a compile-time error to access an instance member through a simple-name. The Main method shows that in a member-access (§7.5.4), instance members must be accessed through instances, and static members must be accessed through types.
10.3.8Nested types
A type declared within a class or struct declaration is called a nested type. A type that is declared within a compilation unit or namespace is called a non-nested type.
In the example
using System;
class A
{
class B
{
static void F() {
Console.WriteLine("A.B.F");
}
}
}
class B is a nested type because it is declared within class A, and class A is a non-nested type because it is declared within a compilation unit.
10.3.8.1Fully qualified name
The fully qualified name (§3.8.1) for a nested type is S.N where S is the fully qualified name of the type in which type N is declared.
10.3.8.2Declared accessibility
Non-nested types can have public or internal declared accessibility and have internal declared accessibility by default. Nested types can have these forms of declared accessibility too, plus one or more additional forms of declared accessibility, depending on whether the containing type is a class or struct:
-
A nested type that is declared in a class can have any of five forms of declared accessibility (public, protected internal, protected, internal, or private) and, like other class members, defaults to private declared accessibility.
-
A nested type that is declared in a struct can have any of three forms of declared accessibility (public, internal, or private) and, like other struct members, defaults to private declared accessibility.
The example
public class List
{
// Private data structure
private class Node
{
public object Data;
public Node Next;
public Node(object data, Node next) {
this.Data = data;
this.Next = next;
}
}
private Node first = null;
private Node last = null;
// Public interface
public void AddToFront(object o) {...}
public void AddToBack(object o) {...}
public object RemoveFromFront() {...}
public object RemoveFromBack() {...}
public int Count { get {...} }
}
declares a private nested class Node.
10.3.8.3Hiding
A nested type may hide (§3.7.1) a base member. The new modifier is permitted on nested type declarations so that hiding can be expressed explicitly. The example
using System;
class Base
{
public static void M() {
Console.WriteLine("Base.M");
}
}
class Derived: Base
{
new public class M
{
public static void F() {
Console.WriteLine("Derived.M.F");
}
}
}
class Test
{
static void Main() {
Derived.M.F();
}
}
shows a nested class M that hides the method M defined in Base.
10.3.8.4this access
A nested type and its containing type do not have a special relationship with regard to this-access (§7.5.7). Specifically, this within a nested type cannot be used to refer to instance members of the containing type. In cases where a nested type needs access to the instance members of its containing type, access can be provided by providing the this for the instance of the containing type as a constructor argument for the nested type. The following example
using System;
class C
{
int i = 123;
public void F() {
Nested n = new Nested(this);
n.G();
}
public class Nested
{
C this_c;
public Nested(C c) {
this_c = c;
}
public void G() {
Console.WriteLine(this_c.i);
}
}
}
class Test
{
static void Main() {
C c = new C();
c.F();
}
}
shows this technique. An instance of C creates an instance of Nested and passes its own this to Nested’s constructor in order to provide subsequent access to C’s instance members.
10.3.8.5Access to private and protected members of the containing type
A nested type has access to all of the members that are accessible to its containing type, including members of the containing type that have private and protected declared accessibility. The example
using System;
class C
{
private static void F() {
Console.WriteLine("C.F");
}
public class Nested
{
public static void G() {
F();
}
}
}
class Test
{
static void Main() {
C.Nested.G();
}
}
shows a class C that contains a nested class Nested. Within Nested, the method G calls the static method F defined in C, and F has private declared accessibility.
A nested type also may access protected members defined in a base type of its containing type. In the example
using System;
class Base
{
protected void F() {
Console.WriteLine("Base.F");
}
}
class Derived: Base
{
public class Nested
{
public void G() {
Derived d = new Derived();
d.F(); // ok
}
}
}
class Test
{
static void Main() {
Derived.Nested n = new Derived.Nested();
n.G();
}
}
the nested class Derived.Nested accesses the protected method F defined in Derived’s base class, Base, by calling through an instance of Derived.
10.3.8.6Nested types in generic classes
A generic class declaration can contain nested type declarations. The type parameters of the enclosing class can be used within the nested types. A nested type declaration can contain additional type parameters that apply only to the nested type.
Every type declaration contained within a generic class declaration is implicitly a generic type declaration. When writing a reference to a type nested within a generic type, the containing constructed type, including its type arguments, must be named. However, from within the outer class, the nested type can be used without qualification; the instance type of the outer class can be implicitly used when constructing the nested type. The following example shows three different correct ways to refer to a constructed type created from Inner; the first two are equivalent:
class Outer
{
class Inner
{
public static void F(T t, U u) {...}
}
static void F(T t) {
Outer.Inner.F(t, "abc"); // These two statements have
Inner.F(t, "abc"); // the same effect
Outer.Inner.F(3, "abc"); // This type is different
Outer.Inner.F(t, "abc"); // Error, Outer needs type arg
}
}
Although it is bad programming style, a type parameter in a nested type can hide a member or type parameter declared in the outer type:
class Outer
{
class Inner // Valid, hides Outer’s T
{
public T t; // Refers to Inner’s T
}
}
10.3.9Reserved member names
To facilitate the underlying C# runtime implementation, for each source member declaration that is a property, event, or indexer, the implementation must reserve two method signatures based on the kind of the member declaration, its name, and its type. It is a compile-time error for a program to declare a member whose signature matches one of these reserved signatures, even if the underlying runtime implementation does not make use of these reservations.
The reserved names do not introduce declarations, thus they do not participate in member lookup. However, a declaration’s associated reserved method signatures do participate in inheritance (§10.3.3), and can be hidden with the new modifier (§10.3.4).
The reservation of these names serves three purposes:
-
To allow the underlying implementation to use an ordinary identifier as a method name for get or set access to the C# language feature.
-
To allow other languages to interoperate using an ordinary identifier as a method name for get or set access to the C# language feature.
-
To help ensure that the source accepted by one conforming compiler is accepted by another, by making the specifics of reserved member names consistent across all C# implementations.
The declaration of a destructor (§10.13) also causes a signature to be reserved (§10.3.9.4).
10.3.9.1Member names reserved for properties
For a property P (§10.7) of type T, the following signatures are reserved:
T get_P();
void set_P(T value);
Both signatures are reserved, even if the property is read-only or write-only.
In the example
using System;
class A
{
public int P {
get { return 123; }
}
}
class B: A
{
new public int get_P() {
return 456;
}
new public void set_P(int value) {
}
}
class Test
{
static void Main() {
B b = new B();
A a = b;
Console.WriteLine(a.P);
Console.WriteLine(b.P);
Console.WriteLine(b.get_P());
}
}
a class A defines a read-only property P, thus reserving signatures for get_P and set_P methods. A class B derives from A and hides both of these reserved signatures. The example produces the output:
123
123
456
10.3.9.2Member names reserved for events
For an event E (§10.8) of delegate type T, the following signatures are reserved:
void add_E(T handler);
void remove_E(T handler);
10.3.9.3Member names reserved for indexers
For an indexer (§10.9) of type T with parameter-list L, the following signatures are reserved:
T get_Item(L);
void set_Item(L, T value);
Both signatures are reserved, even if the indexer is read-only or write-only.
10.3.9.4Member names reserved for destructors
For a class containing a destructor (§10.13), the following signature is reserved:
void Finalize();
Share with your friends: |