search
HomeBackend DevelopmentC#.Net TutorialC# 2.0 Specification (四)

C# 2.0 Specification (四)

Jan 03, 2017 am 10:19 AM

This paragraph is short so I’ll get rid of it first:)

23 Incomplete type

23.1 Incomplete type declaration

The new type modifier partial is used in multiple Define a type in the section. To ensure compatibility with existing programs, this modifier is different from other modifiers (such as get and set) in that it is not a keyword, and it must appear immediately before the keyword class, struct, or interface.

l class-declaration(类声明)
attributes opt class-modifiers opt partialopt class identifier type-parameter-list opt
class-base opt type-parameter-constraints-clausesopt class-body ;opt
(特性 可选 类修饰符 可选 partial可选 class 标识符
类型参数列表 可选 :基类 可选 类型参数约束语句 可选 类体;可选)
l struct-declaration:(结构声明)
attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt
struct-interfacesopt type-parameter-constraints-clausesopt struct-body ;opt
(特性 可选 结构修饰符 可选 partial 可选 struct 标识符
类型参数列表 可选
结构接口 可选 类型参数约束语句 可选
结构体;可选 )
l interface-declaration:(接口声明)
attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt
interface-baseopt type-parameter-constraints-clausesopt interface-body ;opt
(特性可选 接口修饰符 可选 partial 可选 interface 标识符
类型参数列表 可选
基接口 可选 类型参数约束语句 可选
接口体 ;可选)

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 additional parts of the type declaration can exist somewhere else, but the existence of such additional parts is not required; it is also reasonable to include the partial modifier on a single type declaration.



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



Nested types can be declared in multiple places by using the partial modifier. Typically, the containing type (that is, the type containing the nested type) is also declared using partial, 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 Properties

The properties of an incomplete type are determined by combining the properties of the individual parts in an unspecified order. If an attribute is placed in more than one part of an incomplete type, it is equivalent to specifying the attribute multiple times on that type. For example, the attributes on the two partial

[Attr1, Attr2("hello")]
partial class A {}
[Attr3, Attr2("goodbye")]
partial class A {}
等价于如下声明。
[Attr1, Attr2("hello"), Attr3, Attr2("goodbye")]
class A {}

type parameters are also combined in the same style.

23.1.2 Modifiers

When an incomplete type declaration contains access specifications (public, protected, internal and private), it must be consistent with the access specifications of other parts. If no parts of an incomplete type contain access specifications, the type is given appropriate default accessibility (§3.5.1).

If one or more incomplete declarations of a 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 of.



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


When the unsafe modifier is used on an incomplete type declaration, only the specified part is considered unsafe context [unsafe context (§18.1))].

23.1.3 Type parameters and constraints

If a generic type is declared in multiple parts, each part must specify the type parameters. Each part must have the same number of type parameters, and each type parameter must have the same name and order.

When an incomplete generic declaration contains a type parameter constraint (where statement), the constraint must be consistent with the constraints in other parts. In particular, each section containing a constraint must have the same set of type parameters for the constraint, and the set of

class, interface, and constructor constraints must be the same for each type parameter. If no constraints are specified on any part of an incomplete generic, the type parameters are 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 these sections containing constraints effectively specify the same set of classes, interfaces, and constructor constraints for the corresponding same set of type parameters.

23.1.4 Base Classes

When an incomplete class declaration contains a base class description, it must be consistent with all other parts containing a 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 set of base interfaces of a type declared in multiple parts is the union of the base interfaces specified in each part. A specific base interface can be named only once in each section, but the same base interface can be named in multiple sections. But there can be only one implementation for any given base interface member.
In example

partial class C: IA, IB {...}
partial class C: IC {...}
partial class C: IA, IB {...}
中类C的基接口是IA,IB和IC。

通常,在接口声明的部分中提供接口的实现;但这不是必需的。一个部分可以为声明在另一个部分中的接口提供实现。

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

23.1.6成员

声明在多个部分中的类型的成员只是在各个部分中声明的成员的联合。类型声明的所有部分的内容共享相同的声明空间(§3.3),并且每个成员(§3.7)的作用域扩展到所有部分的内容。任何成员的所有可访问域总是包含封闭类型的所有部分;在一个部分中声明的private成员可以随意的在另一个部分访问。在一个类型的多个部分中声明相同的成员将造成编译时错误,除非该成员是一个带有partial修饰符的成员。

partial class A
{
int x; // 错误, 不能多次声明x
partial class Inner // Ok, Inner 是不完整类型
{
int y;
}
}
partial class A
{
int x; // 错误,不能多次声明x
partial class Inner // Ok, Inner是不完整类型
{
int z;
}
}

尽管一个类型中成员的次序对于C#代码并不是太重要,但在面对其他语言和环境时却可能是很重要的。在这样的情况下,在多个部分中声明的类型内成员次序将是未定义的。

23.2名字绑定

虽然可扩展类型的每个部分必须声明在相同的命名空间,但这些部分也可以写在不同的命名空间中。为此,对于各个部分可以使用不同的using指令(§9.3)。当在一个部分中解释简单名字(§7.5.2)时,只有包含该部分的命名空间using 指令被考虑。这将使得在不同部分的相同标识符表示不同的意义。

namespace N
{
using List = System.Collections.ArrayList;
partial class A
{
List x; // x具有类型 System.Collections.ArrayList
}
}
namespace N
{
using List = Widgets.LinkedList;
partial class A
{
List y; // y 具有类型 Widgets.LinkedList
}
}

以上就是C# 2.0 Specification (四)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

Demystifying C# .NET: An Overview for BeginnersDemystifying C# .NET: An Overview for BeginnersApr 20, 2025 am 12:11 AM

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software