c#2.0 Language Specification (v) Incomplete type

Source: Internet
Author: User
Tags abstract constructor contains modifier
Specification Fifth Chapter Incomplete type
5.1 not fully stated
When you define a type that is divided into multiple parts, you use a new type modifier--partial. To ensure compatibility with existing code, this identifier differs from other identifiers: It is not a keyword, as with get and set, and it must appear directly in front of one of the keyword class, struct, and interface.
Class-declaration:
attributesopt class-modifiersopt partialopt class identifier type-parameter-listopt
Class-baseopt type-parameter-constraints-clausesopt Class-body; opt
Struct-declaration:
attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt
Struct-interfacesopt type-parameter-constraints-clausesopt Struct-body; opt
Interface-declaration:
attributesopt interface-modifiersopt partialopt Interface identifier type-parameter-listopt
Interface-baseopt type-parameter-constraints-clausesopt Interface-body; opt
class declaration:
Attribute Optional class modifier Optional partial optional class identifier type argument list optional
base class optional type parameter constraint clause optional class body; optional
Structure declaration:
Attribute optional structure modifier Optional partial optional struct identifier type argument list optional
Structural interface Optional TYPE parameter constraint clause optional structure body; optional
Interface declaration:
Attribute optional interface modifier Optional Partial optional interface identifier type argument list optional
Base interface Optional type parameter constraint clause optional interface body; optional
Each part of an incomplete type declaration must contain the partial modifier and must be in the same namespace as the other parts. The partial modifier indicates that additional parts of the type declaration may be in other locations, but these additional parts are not required, and this runs a separate type declaration containing the partial modifier.
All parts of an incomplete type must be compiled together in order for these parts to be merged during compilation. However, some types do not allow extensions of already compiled types.
Nested types can be declared as multiple parts by using the partial modifier. Typically, a declaration of a type that contains a nested type also uses partial, and the various parts of the abscond type are declared separately in different parts of the containing type.
The partial modifier isn't permitted on delegate or enum declarations.
The partial modifier is not allowed for delegates or enumeration declarations.
5.1.1 Characteristics (attribute)
Attributes on various parts of an incomplete type are merged in an indeterminate order, and if an attribute is placed on more than one part, it is equivalent to a feature used multiple times on the type. For example, the following two sections:
[Attr1, Attr2 ("Hello")]
Partial class A {}
[Attr3, Attr2 ("Goodbye")]
Partial class A {}
Equivalent to the following statement:
[Attr1, Attr2 ("Hello"), ATTR3, Attr2 ("Goodbye")]
Class A {}
Attributes on type parameters are merged in the same way.
5.1.2 modifier
When an incomplete type declaration contains accessibility descriptions (public, protected, internal, and private modifiers), all other parts can contain an identical modifier. If any part of the incomplete type does not contain accessibility instructions, the type will have the appropriate accessibility by default.
If the incomplete declaration of a nested type contains a new modifier, the warning will not appear when the nested type hides the inherited member.
If one or more parts of a class's incomplete declaration contain an abstract modifier, the class is considered abstract. Otherwise, this class is considered to be non-abstract.
This class is considered sealed if one or more parts of the incomplete declaration of the class contain the sealed modifier. Otherwise, this class is considered to be unsealed.
Note that a class cannot be both abstract and sealed.
When the unsafe modifier is used on one part of an incomplete type declaration, only this particular part is considered to be in an unsafe environment.
5.1.3 type parameters and constraints
If a subtype type is declared in more than one part, each part must declare the type parameter. Each part must have the same number of type parameters, and the name and order of each type parameter must be the same.
If an incomplete generic type contains a type parameter constraint (a WHERE clause), the same constraint can also be included in other parts. However, each part of the containing constraint must constrain the collection of the same type parameter, and the class, interface, and constructor constraints of each type parameter in the collection must be the same. If a type parameter constraint is not specified for each part of an incomplete generic type, these type parameters are considered unconstrained.
The following example
Partial class Dictionary<k,v>
where k:icomparable<k>
where V:ikeyprovider<k>, ipersistable
{
...
}
Partial class Dictionary<k,v>
where v:ipersistable, ikeyprovider<k>
where k:icomparable<k>
{
...
}
Partial class Dictionary<k,v>
{
...
}
is correct because the part that contains the constraint (the first and the second) effectively specifies the same set of classes, interfaces, and constructor constraints for the same set of type parameters.
5.1.4 base class
When an incomplete class declaration contains the specified base class, each part is allowed to contain the same specified base class. If no part of an incomplete type specifies a base class, the base class for the class is System.Object.
5.1.5 Base Interface
A base interface declared separately in a different section is a union that specifies the base interface on each part. A specific base interface can only be named once in each section, but allows the same interface to be named in more than one section. Any member in the base interface can only be implemented once.
In the example
In the following example
Partial class C:ia, IB {...}
Partial class C:ic {...}
Partial class C:ia, IB {...}
The base interface set for Class C is IA, IB, and IC.
Typically, a section provides only one implementation for the interface declared on that part, however, this is not necessary. One section can provide implementations for interfaces declared on another different section:
Partial class X
{
int IComparable.CompareTo (object o) {...}
}
Partial class X:icomparable
{
...
}
5.1.6 Members
Members declared in more than one section are simply aggregations of the members declared in each section. The class body in all parts of a type declaration shares the same declaration space, and each member's scope runs through all parts. The accessibility of any member always includes all parts of the enclosing type, and private members declared in one section can be accessed freely in another part. If the same member is declared in more than one section, a compilation error occurs, unless the member is a type declared with the partial modifier.
Partial class A
{
int x; Error, cannot declare x multiple times
Partial class Inner//Correct, this is an incomplete inner type
{
int y;
}
}
Partial class A
{
int x; Error, cannot declare x multiple times
Partial class Inner//Correct, this is an incomplete inner type
{
int z;
}
}
Although the order of members is not important for C # code, it may be important to interface with other languages or environments. In this case, the order of the members declared in multiple parts of the type is undefined.
5.2 Name Binding
Although parts of an extensible type must be declared in the same namespace, different namespace declarations can be written in various sections. Therefore, different using directives can appear in various sections. When a simple name is interpreted in a section, only the using directive in the namespace declared in that section is considered.
Namespace N
{
Using List = System.Collections.ArrayList;
Partial class A
{
List x; The type of x is System.Collections.ArrayList
}
}
Namespace N
{
Using List = Widgets.linkedlist;
Partial class A
{
List y; The type of y is widgets.linkedlist
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.