Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Delegate creation expressions

Invocations on boxed instances | Member access | Method invocations | Extension method invocations | Element access | Indexer access | Postfix increment and decrement operators | Object creation expressions | Object initializers | Collection initializers |


Читайте также:
  1. Absence of the articles in set expressions
  2. Additional Words and Expressions
  3. And some useful expressions
  4. Anonymous function expressions
  5. Anonymous object creation expressions
  6. Array creation expressions
  7. Assignment 5. Fill in the gaps with the words and expressions from the text.

A delegate-creation-expression is used to create a new instance of a delegate-type.

delegate-creation-expression:
new delegate-type (expression)

The argument of a delegate creation expression must be a method group, an anonymous function or a value of either the compile time type dynamic or a delegate-type. If the argument is a method group, it identifies the method and, for an instance method, the object for which to create a delegate. If the argument is an anonymous function it directly defines the parameters and method body of the delegate target. If the argument is a value it identifies a delegate instance of which to create a copy.

If the expression has the compile-time type dynamic, the delegate-creation-expression is dynamically bound (§7.2.2), and the rules below are applied at run-time using the run-time type of the expression. Otherwise the rules are applied at compile-time.

The binding-time processing of a delegate-creation-expression of the form new D(E), where D is a delegate-type and E is an expression, consists of the following steps:

· If E is a method group, the delegate creation expression is processed in the same way as a method group conversion (§6.6) from E to D.

· If E is an anonymous function, the delegate creation expression is processed in the same way as an anonymous function conversion (§6.5) from E to D.

· If E is a value, E must be compatible (§15.1) with D, and the result is a reference to a newly created delegate of type D that refers to the same invocation list as E. If E is not compatible with D, a compile-time error occurs.

The run-time processing of a delegate-creation-expression of the form new D(E), where D is a delegate-type and E is an expression, consists of the following steps:

· If E is a method group, the delegate creation expression is evaluated as a method group conversion (§6.6) from E to D.

· If E is an anonymous function, the delegate creation is evaluated as an anonymous function conversion from E to D (§6.5).

· If E is a value of a delegate-type:

o E is evaluated. If this evaluation causes an exception, no further steps are executed.

o If the value of E is null, a System.NullReferenceException is thrown and no further steps are executed.

o A new instance of the delegate type D is allocated. If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed.

o The new delegate instance is initialized with the same invocation list as the delegate instance given by E.

The invocation list of a delegate is determined when the delegate is instantiated and then remains constant for the entire lifetime of the delegate. In other words, it is not possible to change the target callable entities of a delegate once it has been created. When two delegates are combined or one is removed from another (§15.1), a new delegate results; no existing delegate has its contents changed.

It is not possible to create a delegate that refers to a property, indexer, user-defined operator, instance constructor, destructor, or static constructor.

As described above, when a delegate is created from a method group, the formal parameter list and return type of the delegate determine which of the overloaded methods to select. In the example

delegate double DoubleFunc(double x);

class A
{
DoubleFunc f = new DoubleFunc(Square);

static float Square(float x) {
return x * x;
}

static double Square(double x) {
return x * x;
}
}

the A.f field is initialized with a delegate that refers to the second Square method because that method exactly matches the formal parameter list and return type of DoubleFunc. Had the second Square method not been present, a compile-time error would have occurred.


Дата добавления: 2015-11-16; просмотров: 59 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Array creation expressions| Anonymous object creation expressions

mybiblioteka.su - 2015-2024 год. (0.005 сек.)