X hits on this document

919 views

0 shares

0 downloads

0 comments

214 / 396

Chapter ‎18   Unsafe code

10.2.2 The 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.2.3 Access 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.2.4 Constituent 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.2.5 Static 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 classes and instance members as belonging to objects (instances of classes).

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. No matter how many instances of a class 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;

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.201

Document info
Document views919
Page views919
Page last viewedWed Dec 07 11:17:23 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments