C # 2.0 specification (iv)

Source: Internet
Author: User


This is a short time to kill first:)



23 Incomplete type



23.1 Incomplete type declaration



The new type modifier, partial, is used to define a type in multiple parts. To ensure compatibility with existing programs, this modifier is different from other modifiers (such as Get and set), 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
(Features optional class modifier optional partial optional class identifier
Type parameter list optional: base class optional type parameter constraint statement optional class body; optional)
l struct-declaration: (struct declaration)
attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt
struct-interfacesopt type-parameter-constraints-clausesopt struct-body; opt
(Features optional structure modifier optional partial optional struct identifier
Type parameter list optional
Structural interface Optional Type parameter constraint statement Optional
Structure; optional)
l interface-declaration: (interface declaration)
attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt
interface-baseopt type-parameter-constraints-clausesopt interface-body; opt
(Feature optional interface modifier optional partial optional interface identifier
Type parameter 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 must be declared in the same namespace as the other parts. The partial modifier indicates that the additional part of the type declaration can exist somewhere else, but the existence of this additional part is not necessary; it is reasonable to include the partial modifier in a single type declaration.



All parts of incomplete types must be compiled together so that they can be merged at compile time. In particular, incomplete types are not allowed to extend types that have already been compiled.



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

The partial modifier cannot be used in delegate or enumeration declarations.


23.1 Features

The characteristics of the incomplete type are determined by combining the characteristics of the various parts in an unspecified order. If a feature is placed in multiple parts of an incomplete type, it is equivalent to specifying the feature multiple times on the type. For example, these two parts


[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 {}
The characteristics on the type parameter are also combined in the same style.

23.1.2 Modifier

When an incomplete type declaration contains access instructions (public, protected, internal, and private), it must be consistent with the access instructions of other parts. If each part of an incomplete type does not contain access instructions, the type will be given appropriate default accessibility (§3.5.1).

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



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



Note that a class cannot be both abstract and sealed at the same time.


When the unsafe modifier is used for an incomplete type declaration, only certain parts are considered unsafe up and down [unsafe contex (§18.1))].


23.1.3 Type parameters and constraints

If a generic type is declared in multiple parts, each part must specify the type parameter. Each part 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 (where statement), the constraint must be consistent with other constraints. In particular, each part that contains constraints must have the same set of type parameter constraints, and for each type parameter,

The set of class, interface, and constructor constraints must be the same. If no part of the incomplete generic is specified with constraints, the type parameter is considered unconstrained.
Examples

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>
{
...
}
This is correct because these constraints-containing parts effectively specify the same set of classes and interfaces, and the constructor constraints for the corresponding type parameters of the same set.

23.1.4 Base class

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

23.1.5 Base interface

The set of base interfaces of types declared in multiple parts is a union of the base interfaces specified in each part. A specific base interface can only be named once in each part, but the same base interface can be named in multiple parts. However, there can be only one 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 interfaces of Class C are IA, IB and IC.
Usually, the implementation of the interface is provided in the interface declaration section; but this is not required. One part can provide an implementation for an interface declared in another part.

partial class X
{
int IComparable.CompareTo (object o) {...}
}
partial class X: IComparable
{
...
}
23.1.6 Members

Members of types declared in multiple parts are just a union of members declared in each part. The contents of all parts of a type declaration share the same declaration space (§3.3), and the scope of each member (§3.7) extends to the contents of all parts. All accessible domains of any member always contain all parts of the closed type; private members declared in one part can be accessed in another part at will. Declaring the same member in multiple parts of a type will cause a compile-time error unless the member is a member with a partial modifier.

partial class A
{
int x; // Error, x cannot be declared multiple times
partial class Inner // Ok, Inner is an incomplete type
{
int y;
}
}
partial class A
{
int x; // Error, x cannot be declared multiple times
partial class Inner // Ok, Inner is an incomplete type
{
int z;
}
}
Although the order of members in a type is not too important for C # code, it may be very important when facing other languages and environments. In such cases, the order of members within the type declared in multiple sections 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. For this purpose, different using instructions (§9.3) can be used for each part. When interpreting simple names in a section (§7.5.2), only the using directives of the namespace containing the section are considered. This will make the same identifier in different parts have 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
}
}
The above is the content of C # 2.0 Specification (4). For more related content, please pay attention to topic.alibabacloud.com (www.php.cn)!

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.