C # 2.0 specification (d)

Source: Internet
Author: User
Tags abstract constructor modifier modifiers
This is a short time to kill the first:
23 Incomplete types
23.1 Incomplete type declarations
The new type modifier partial is used to define a type in more than one section. To ensure compatibility with existing programs, this modifier and other modifiers (such as Get and set) are different, it is not a keyword, and it must precede the keyword class, struct, or interface.

L Class-declaration (class declaration)
attributes opt class-modifiers opt partialopt class identifier type-parameter-list opt
Class-base opt type-parameter-constraints-clausesopt class-body; opt

(attribute optional class modifier optional partial class identifier

Type parameter list Optional: base class optional type parameter constraint statement optional class body; optional)

L Struct-declaration: (structure Declaration)
attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt
Struct-interfacesopt type-parameter-constraints-clausesopt Struct-body; opt

(attribute optional structure modifier Optional partial optional struct identifier

Type argument list optional

Structural interface Optional type parameter constraint statement optional

structural body; optional)

L Interface-declaration: (interface Declaration)
attributesopt interface-modifiersopt partialopt Interface identifier type-parameter-listopt
Interface-baseopt type-parameter-constraints-clausesopt Interface-body; opt

(attribute optional interface modifier Optional partial optional interface identifier

Type argument list optional

Base interface Optional type parameter constraint statement optional

interface body; optional)

Each part of an incomplete type declaration must contain the partial modifier, and the other parts must be declared in the same namespace. The partial modifier indicates that the additional part of the type declaration can exist somewhere else, but the existence of such additional part is not required, and it is reasonable to include the partial modifier in a single type declaration.



All parts of an incomplete type must be compiled together so that they can be merged at compile time. In particular, incomplete types do not allow extensions to types that have been compiled.



Nested types (nested type) can be declared in multiple places by using the partial modifier. Typically, the containing type (that is, the type that contains the nested type) also uses the partial declaration, and the parts of the nested type are declared in different parts of the containing type.

The partial modifier cannot be used in a delegate or enumeration declaration.



23.1 characteristics
Attributes of an incomplete type are determined by combining the attributes of each part in an indefinite (unspecified) order. If an attribute is placed in multiple parts of an incomplete type, it is equivalent to specifying the attribute multiple times on that type. For example, these two parts

[Attr1, Attr2 ("Hello")]
Partial class A {}

[Attr3, Attr2 ("Goodbye")]
Partial class A {}

is equivalent to the following declaration.

[Attr1, Attr2 ("Hello"), ATTR3, Attr2 ("Goodbye")]
Class A {}



Attributes on type parameters are also grouped in the same style.

23.1.2 modifier
When an incomplete type declaration contains an Access description (Public,protected,internal and private), it must be consistent with the access description of the other part. If parts of an incomplete type do not contain access instructions, the type is given the appropriate default accessibility (§3.5.1).

If one or more incomplete declarations of a nested type contain a new modifier, and if a nested type hides an inherited member, there will be no warning. (§3.7.12)



If one or more incomplete declarations of a class contain an abstract modifier, then the class is abstract (§10.1.1.1), and the other is non-abstract.



Note that a class cannot be both abstract and sealed (sealed).



When the unsafe modifier is used for an incomplete type declaration, only a specific part is considered unsafe up and down [unsafe Contex (§18.1)].



23.1.3 type parameters and constraints
If a generic type is declared in more than one part, each part must describe the type parameter. Each section must have the same number of type parameters, and must have the same name and order for each type parameter.



When an incomplete generic declaration contains a type parameter constraint (a WHERE statement), the constraint must conform to the constraints of the other parts. In particular, each part of the containing constraint must have a constraint of the same collection type parameter, and for each type parameter,



The collection of class, interface, and constructor constraints must be the same. If any part of the incomplete generic does not specify a constraint, the type parameter is considered unconstrained.

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 effectively specifies the same set of classes, interfaces, and the constructor constraints of the type parameters of the corresponding collection.

23.1.4 base class
When an incomplete class declaration contains a base class description, it must be consistent with all the other parts that contain the base class description. If any part of the incomplete class declaration does not contain a base class declaration, then the base class will be System.Object (§10.1.2.1).

23.1.5 Base Interface
The base interface collection of types declared in multiple sections is the union of the base interfaces specified in each section. A specific base interface can only be named once in each section, but it is possible to name the same base interface in more than one section. However, there can only be a unique implementation for any given base interface member.

In the example

Partial class C:ia, IB {...}

Partial class C:ic {...}

Partial class C:ia, IB {...}

The base interface for Class C in is Ia,ib and IC.



In general, the implementation of the interface is provided in the part of the interface declaration, but this is not required. A section can provide implementations for the interfaces declared in another section.

Partial class X
{
int IComparable.CompareTo (object o) {...}
}

Partial class X:icomparable
{
...
}

23.1.6 Members
A member of a type declared in more than one part is only a union of the members declared in each section. The contents of all parts of a type declaration share the same declaration space (§3.3), and the scope of each member (§3.7) expands to the contents of all parts. All the accessible domains of any member always contain all parts of the enclosing type; Private members declared in one section can be accessed at random in another section. Declaring the same member in more than one part of a type will result in a compile-time error unless the member is a member with a partial modifier.

Partial class A
{
int x; Error, cannot declare x multiple times

Partial class Inner//Ok, Inner is an incomplete type
{
int y;
}
}

Partial class A
{
int x; Error, cannot declare x multiple times

Partial class Inner//Ok, Inner is an incomplete type
{
int z;
}
}

Although the order of members in a type is not important for C # code, it may be important in the face of other languages and environments. In such cases, the member order within the type declared in more than one section will be undefined.



23.2 Name Binding
Although each part of an extensible type must be declared in the same namespace, these parts can also be written in different namespaces. To do this, you can use different using directives (§9.3) for each part. When a simple name (§7.5.2) is interpreted in a section, only the namespace-using instruction containing that part is considered. This will make the same identifiers in different parts represent different meanings.

Namespace N
{
Using List = System.Collections.ArrayList;

Partial class A
{
List x; X has type System.Collections.ArrayList
}
}

Namespace N
{
Using List = Widgets.linkedlist;

Partial class A
{
List y; Y has type 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.