Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 13 Conversions

1Find the most specific target type, TX, of the operators in U:

2o If any of the operators in U convert to T, then TX is T.

3o Otherwise, if any of the operators in U convert to types that are encompassed by T, then TX is the

4most encompassing type in the combined set of target types of those operators. If no most

5encompassing type can be found, then the conversion is ambiguous and a compile-time error occurs.

6o Otherwise, TX is the most encompassed type in the combined set of target types of the operators in U.

7If no most encompassed type can be found, then the conversion is ambiguous and a compile-time

8error occurs.

9If U contains exactly one user-defined conversion operator that converts from SX to TX, then this is the

10most specific conversion operator. If no such operator exists, or if more than one such operator exists,

11then the conversion is ambiguous and a compile-time error occurs. Otherwise, the user-defined

12conversion is applied:

13o If S is not SX, then a standard explicit conversion from S to SX is performed.

14o The most specific user-defined conversion operator is invoked to convert from SX to TX.

15o If TX is not T, then a standard explicit conversion from TX to T is performed.

1613.5 Anonymous method conversions

17An implicit conversion (§13.1) exists from an anonymous-method-expression to any compatible delegate

18type. If D is a delegate type, and A is an anonymous-method-expression, then D is compatible with A if and

19only if the following two conditions are met.

20First, the parameter types of D shall be compatible with A:

21o If A does not contain an anonymous-method-signature, then D can have zero or more parameters of

22any type, as long as no parameter of D has the out parameter modifier.

23o If A has an anonymous-method-signature, then D shall have the same number of parameters and each

24parameter of A shall be compatible with the corresponding parameter of D. A parameter of A is

25considered compatible with a parameter of D if they are both of the same type and the presence or

26absence of the ref or out modifier on the parameter of A matches the corresponding parameter of

27D. Whether the final parameter of D is a parameter-array is not considered when determining the

28compatibility of A and D. A parameter which has the parameter-array modifier is compatible with a

29parameter without the parameter-array modifier if they are both of the same type.

30Second, the return type of D shall be compatible with A. For these rules, A is not considered to contain

31the block of any other anonymous methods:

32o If D is declared with a void return type, then any return statement contained in A shall not specify

33an expression.

34o If D is declared with a return type of R, then any return statement contained in A shall specify an

35expression which is implicitly convertible (§13.1) to R. Furthermore, the end-point of the block of A

36shall not be reachable.

37Besides the implicit conversions to compatible delegate types, no other conversions exist from an

38anonymous-method-expression, not even to the type object.

39[Example: The following examples illustrate these rules:

40delegate void D(int x);

41

D d1 =

delegate { };

// Ok

42

D d2 =

delegate() { };

// Error, signature mismatch

43

D d3

=

delegate(long x) { };

// Error, signature mismatch

44

D d4

=

delegate(int x) { };

// Ok

45

D d5

=

delegate(int x) { return; };

// Ok

46

D d6

=

delegate(int x) { return x; };

// Error, return type mismatch

141

 

C# LANGUAGE SPECIFICATION

 

1

delegate void E(out int x);

 

2

E e1 = delegate { };

// Error, E has an out parameter

3

E e2 = delegate(out int x) { x = 1; };

// Ok

4

E e3 = delegate(ref int x) { x = 1; }; // Error, signature mismatch

5

delegate int P(params int[] a);

 

6

P p1 = delegate { };

// Error, end of block reachable

7

P p2

= delegate { return; };

// Error, return type mismatch

8

P p3

= delegate { return 1; };

// Ok

9

P p4

= delegate { return "Hello"; };

// Error, return type mismatch

10

P p5

= delegate(int[] a) {

// Ok

11return a[0];

12};

13

P p6 = delegate(params int[] a) {

// Error, params modifier

14return a[0];

15};

16

P p7 = delegate(int[] a) {

// Error, return type mismatch

17

if (a.Length > 0) return a[0];

 

18return "Hello";

19};

20delegate object Q(params int[] a);

21

Q q1 = delegate(int[] a) {

// Ok

22

if (a.Length > 0) return a[0];

 

23return "Hello";

24};

25end example]

26A delegate-creation-expression (§14.5.10.3) can be used as an alternate syntax for converting an anonymous

27method to a delegate type.

2813.6 Method group conversions

29Similar to the implicit anonymous method conversions described in §13.5, an implicit conversion exists

30from a method group (§14.1) to a compatible delegate type. If D is a delegate type, and E is an expression

31that is classified as a method group, then D is compatible with E if and only if E contains at least one method

32that is applicable in its normal form (§14.4.2.1) to any argument list (§14.4.1) having types and modifiers

33matching the parameter types and modifiers of D.

34The compile-time application of the conversion from E to D is the same as the compile-time processing of

35the delegate creation expression new D(E) (§14.5.10.3). Note that the existence of an implicit conversion

36from E to D just indicates that the set of applicable methods is not empty, but does not guarantee that the

37compile-time application of the conversion will succeed without error.

38[Example: In the following code

39using System;

40using System.Windows.Forms;

41class AlertDialog

42{

43

Label message = new Label();

44

Button okButton = new Button();

45

Button cancelButton = new Button();

46

public AlertDialog() {

47

okButton.Click += new EventHandler(OkClick);

48

cancelButton.Click += new EventHandler(CancelClick);

49

50

}

51

void OkClick(object sender, EventArgs e) {

52

53

}

142

Chapter 13 Conversions

1

void CancelClick(object sender, EventArgs e) {

2

3}

4}

5the constructor creates two delegate instances using the new operator. Implicit method group conversions

6permit this to be shortened to

7public AlertDialog() {

8

okButton.Click += OkClick;

9

cancelButton.Click += CancelClick;

10

11}

12end example]

13As with all other implicit and explicit conversions, the cast operator can be used to explicitly perform a

14particular conversion. [Example: Thus, the example

15object obj = new EventHandler(myDialog.OkClick);

16could instead be written

17object obj = (EventHandler)myDialog.OkClick;

18end example]

19Although method groups and anonymous method expressions can influence overload resolution, they do not

20participate in type inferencing (§26.6.4).

143

Соседние файлы в предмете Электротехника