22.1 Delegate Declarations

xiaoxiao2021-04-05  277

A delegate-declaration is a type-declaration (§16.5) That Declares a New

Delegate Type.

Delegate-Declaration:

AttributeSopt delegate-modifiersopt delegate Return-Type Identifier

FORMAL-parameter-listopt;

Delegate-modifiers:

Delegate-Modifier

Delegate-Modifier Delegate-Modifier

Delegate-Modifier:

New

public

protected

Internal

Private

IT is a compile-time error for the Same Modifier to Appear Multiple Times

IN a Delegate Declaration.

The New Modifier Is Only Permitted On Delegates Declared Withnother

Type, In Which Case It Specifies That Such

A delegate hides an inherited member by The Same Name, As Described in §17.2

.2.

The Public, Protected, Internal, And Private Modifiers Control THE

Accessibility of the delegate Type.

Depending on the context in which the delegate Declaration Occurs, Some of

THESE MODIFIERS May Not Be Permitted

(§10.5.1).

The delegate? S Type Name is identifier.

The Optional Formal-Parameter-List Specifies the Parameters of The Parameters of the

Delegate, and return-type indeicates the return

Type of the delegate. a Method and a delegate Type Are Compatible if Both

Of the folowing is true:

? The Have The Same Number Or Parameters, with the Same Types, in The Same

ORDER, with the Same Parameter

Modifier.

. Their return-type is is.

Delegate Types in C # Are Name Equivalent, Not Structurally Equivalent.

[NOTE: HOWEVER, Instances of Two Distinct

But Structurally Equivalent Delegate Types May Compare As Equal (14.9.8).

End Note] Specifically, Two Different

Delegate Types That Have The Same Parameter Lists and Return Type Are

Considered DiffERENT DELEGATE TYPES.

[EXAMPLE: for EXAMPLE:

C # language specification

298

DELEGATE INT D1 (INT I, DOUBLE D);

Class A

{

Public Static Int M1 (Int A, Double B) {?}}

Class B

{

DELEGATE INT D2 (INT C, DOUBLE D);

Public Static Int M1 (INT F, DOUBLE G) {?}

Public Static Void M2 (INT K, DOUBLE L) {?}

PUBLIC Static Int M3 (INT G) {?}

Public Static Void M4 (INT G) {?}

}

The DLELEGATE TYPES D1 and D2 Are Both compatible with the methods a.m1 and

B.M1, Since They Have the Same

Return Type and Parameter List; However, There Delegate Types Are Two

Different Types, So They Are Not

Interchangeable. The delegate Types D1 and D2 Are Incompatible with the

Methods B.M2, B.M3, And B.M4, Since

Theh Have DiffERENT RETURN TYPES or Parameter Lists. End Example]

The only way to declare a delegate Type Is Via a delegate-declaration. A

Delegate Type Is a class type what is IS

Derived from system.delegate. delegate Types Are Implicitly Sealed, SO IT

Is Not Permissible to Derive Any

Type from a delegate Type. it is also not permissible to derive a

Non-delegate Class Type from system.delegate.

System.Delegate is not itself a delegate type; it is a class type from ip

Which all delegate Types Are Derived.

C # provides Special Syntax for Delegate Instantiation and Invocation.

Except for instantiation, any Operation That

Can Be Applied to a Class or Class Instance Can Also Be Applied TO A

Delegate Class or Instance, Respectively. in

Particular, IT IS Possible to Access Members of The System.dlegate Type

Via The USUAL MEMBER Access Syntax.

The set of methods encapsulated by a delegate instance is caled an

Invocation List. When a Delegate Instance IS

Created (22.2) from a single method, it encapsulates That Method, And ITS

Invocation List Contains Only One Entry.

However, When Two Non-Null Delegate Instances Are Combined, Their

INVOCATION LISTS Are Concatenated? In Theorder Left Operand Then Right Operand? To form a new invocation list, Which

Contains two or more entries.

Delegates Are Combined Using The Binary (§14.7.4) and = Operators (14.1

3.2). A Delegate Can Be Removed

From a combination of delegates, using the binary - (§14.7.5) and - =

Operators (14.13.2). Delegates Can Be

Compared for equality (14.9.8).

[EXAMPLE: The Following Example Shows The Instantiation of A Number of

Delegates, and their corresponding

Invocation Lists:

Delegate Void D (int X);

Class test

{

PUBLIC Static Void M1 (INT I) {?}

PUBLIC Static Void M2 (INT I) {?}

}

Class Demo

{

Static void main () {

D cd1 = new d (test.m1); // m1

D CD2 = New D (Test.m2); // m2

D CD3 = CD1 CD2; // m1 m2

D CD4 = CD3 CD1; // m1 m2 m1

D CD5 = CD4 CD3; // M1 M2 M1 M1 M2

}

}

When CD1 and CD2 Are Instantiated, They Each Encapsulate One Method. By

CD3 is instantiated, IT HAS AN

Invocation List of Two Methods, M1 and M2, IN That Order. CD4? S Invocation

List Contains M1, M2, And M1, in That

Order. Finally, CD5? S INVOCATION List Contains M1, M2, M1, M1, AND M2, IN

That Order.

For more examples of combining (as well as removing) delegates, see §22.3.

End example]

转载请注明原文地址:https://www.9cbs.com/read-131987.html

New Post(0)