Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

CATEGORII DOCUMENTE




loading...



AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml


Class members

C sharp

+ Font mai mare | - Font mai mic








DOCUMENTE SIMILARE

Trimite pe Messenger
Conditional operator
Selection statements
The try statement
The empty statement
Reference types: The string type
Value types: Struct types, Simple, Integral, Floating
Interfaces
Indexers
Using directives
Variables and parameters

Class 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 are divided into the following categories:

Constants, which represent constant values associated with the class (§10.3).

Fields, which are the variables of the class (§10.4).

Methods, which implement the computations and actions that can be performed by the class (§10.5).

Properties, which define named attributes and the actions associated with reading and writing those attributes (§10.6).

Events, which define notifications that can be generated by the class (§10.7).

Indexers, which permit instances of the class to be indexed in the same way as arrays (§10.8).

Operators, which define the expression operators that can be applied to instances of the class (§10.9).

Instance constructors, which implement the actions required to initialize instances of the class (§10.10)

Destructors, which implement the actions to be performed before instances of the class are permanently discarded (§10.12).

Static constructors, which implement the actions required to initialize the class itself (§10.11).

Types, which represent the types that are local to the class (§9.5).

Members that can contain executable code are collectively known as the function members of the class (7.4).

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, static constructors and destructors 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.

The signature of an instance constructor must differ from the signatures of all other instance constructors declared in the same class.

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 (§10.2.1) 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).

Inheritance

A class inherits the members of its direct base class. Inheritance means that a class implicitly contains all members of its direct base class, except for the instance constructors, static constructors, and destructors 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, static constructors, and destructors 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 hide3.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 the member—it merely makes the member inaccessible in 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.4) 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 the function member is invoked.

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 is issued. This warning is suppressed by removing the new modifier.

It is an error to use the new and override modifiers in the same declaration.

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 an error to specify more than one access modifier. When a class-member-declaration does not include any access modifiers, private is assumed.



Constituent types

Types that are used in the declaration of a member are called the constituent types of the 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 the member itself (§3.5.4).

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 is referenced in a member-access7.5.4) of the form E.M, E must denote a type that has a member M. It is an 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 does not operate on a specific instance, and it is an 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 is referenced in a member-access7.5.4) of the form E.M, E must denote an instance of a type that has a member M. It is an 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 operates on a given instance of the class, and this instance can be accessed as this7.5.7).

The following example illustrates the rules for accessing static and instance members:

class Test

     static void G()

     static void Main()
}

The F method shows that in an instance function member, a simple-name7.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 an error to access an instance member through a simple-name. The Main method shows that in a member-access7.5.4), instance members must be accessed through instances, and static members must be accessed through types.

Nested types

A type declared within a class or struct 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

class A

     }
}

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.

Fully 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 N is declared.

Declared accessibility

Non-nested types can have public or internal declared accessibility and default to internal declared accessibility. Nested types can have these forms of declared accessibility 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 the 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 Node first = null;
     private Node last = null;

     // Public interface

     public void AddToFront(object o)

     public void AddToBack(object o)

     public object RemoveFromFront()

     public object AddToFront()

     public int Count { get }

}

declares a private nested class Node.

Hiding

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

}

class Derived: Base

     }
}

class Test

}

shows a nested class M that hides the method M defined in Base.

this access

A nested type and its containing type do not have a special relationship with regard to this-access7.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. In the example

using System;

class C

     public class Nested

           public void G()
     }
}

class Test
}

shows this technique. A C instance 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.

Access 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

     public class Nested
    
     }
}

class Test

}

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

}

class Derived: Base

     }
}

class Test

}

the nested class Derived.Nested accesses the protected method F defined in Derived’s base class, Base, by calling through an instance of Derived.

Reserved 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 an 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.2.1), and can be hidden with the new modifier (§10.2.2).

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.12) also causes a signature to be reserved (§10.2.7.4).

Member Names Reserved for Properties

For a property P10.6) 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.

Member Names Reserved for Events

For an event E10.7) of delegate type T, the following signatures are reserved:

void add_E(T handler);
void remove_E(T handler);

Member Names Reserved for Indexers

For an indexer (§10.8) 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.

Member Names Reserved for Destructors

For a class containing a destructor (§10.12), the following signature is reserved:



loading...






Politica de confidentialitate

DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 473
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2020 . All rights reserved

Distribuie URL

Adauga cod HTML in site