14.5.10.3 Delegate creation expressions

来源:互联网 发布:win7系统内存优化 编辑:程序博客网 时间:2024/06/06 18:15
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 (?4.1)
or a value of a delegatetype.
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 a value of a delegate-type,
it identifies a delegate instance of
which to create a copy.
The compile-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 set of methods identified by E must include exactly one method that is
compatible (?2.1) with D, and
this method becomes the one to which the newly created delegate refers. If
no matching method exists, or if
more than one matching method exists, a compile-time error occurs. If the
selected method is an instance
method, the instance expression associated with E determines the target
object of the delegate.
As in a method invocation, the selected method must be compatible with the
context of the method group: If
the method is a static method, the method group must have resulted from a
simple-name or a member-access
through a type. If the method is an instance method, the method group must
have resulted from a simplename
or a member-access through a variable or value. If the selected method does
not match the context of
the method group, a compile-time error occurs.
The result is a value of type D, namely a newly created delegate that
refers to the selected method and target
object.
?Otherwise, if E is a value of a delegate-type:
D and E must be compatible (?2.1); otherwise, a compile-time error occurs.
The result is a value of type D, namely a newly created delegate that
refers to the same invocation list as E.
?Otherwise, the delegate creation expression is invalid, and 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:
If the method selected at compile-time is a static method, the target
object of the delegate is null.
Otherwise, the selected method is an instance method, and the target object
of the delegate is determined
from the instance expression associated with E:
?The instance expression is evaluated. If this evaluation causes an
exception, no further steps are
executed.
?If the instance expression is of a reference-type, the value computed by
the instance expression
becomes the target object. If the target object is null, a
System.NullReferenceException
is thrown and no further steps are executed.
?If the instance expression is of a value-type, a boxing operation (?1.3.1)
is performed to
convert the value to an object, and this object becomes the target object.
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.
C# LANGUAGE SPECIFICATION
150
The new delegate instance is initialized with a reference to the method
that was determined at compile-time
and a reference to the target object computed above.
?If E is a value of a delegate-type:
E is evaluated. If this evaluation causes an exception, no further steps
are executed.
If the value of E is null, a System.NullReferenceException is thrown and no
further steps are
executed.
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.
The new delegate instance is initialized with references to the same
invocation list as the delegate instance
given by E.
The method and object to which a delegate refers are determined when the
delegate is instantiated and then
remain constant for the entire lifetime of the delegate. In other words, it
is not possible to change the target
method or object of a delegate once it has been created. [Note: Remember,
when two delegates are
combined or one is removed from another, a new delegate results; no
existing delegate has its content
changed. end note]
It is not possible to create a delegate that refers to a property, indexer,
user-defined operator, instance
constructor, destructor, or static constructor.
[Example: 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. end example]
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一个月染了6次头怎么办 五0二干在衣服上怎么办 刚怀孕见红了肚子不痛怎么办 我有外遇了老婆不离婚怎么办 套了牙套的牙疼怎么办 我鼻子上有很多螨虫和黑头怎么办 鱼刺卡在喉咙怎么办最有效的办法 脚被蚊子咬了很痒怎么办 好压7z密码忘了怎么办 4g卡显示2g网络怎么办 过塑机把纸吞了怎么办 红米1s开不了机怎么办 跟老婆吵架闹的要离婚该怎么办 充了q币没有到账怎么办 9个月宝宝吃了盐怎么办 红米4x开不了机怎么办 鱼身上有红斑像出血了怎么办 草鱼身上有红斑像出血了怎么办 宝宝屁眼红的破皮了怎么办 孩子身上起红疙瘩很痒怎么办 久而不射,但软了怎么办 盆底综合肌力1级怎么办 头发掉的厉害怎么办吃什么好 给蜂蛰了肿了痒怎么办 小米手环2没电了怎么办 小米手环2不亮了怎么办 红米3s无限重启怎么办 乐视手机1s卡顿怎么办 老公出轨了怎么办你会选择离婚吗 c盘和d盘换换了怎么办 晚上2点到3点醒怎么办 红米3s变砖了怎么办 6s锁屏密码忘了怎么办 怀孕9个月了胃疼怎么办 怀孕6个月了胃疼怎么办 孕妇胃疼怎么办4个月了 25岁欠了5万块钱怎么办 感冒嗓子疼怎么办最简单的方法 和老婆离婚了我的心好痛怎么办 4s店不给退定金怎么办 教你闪腰了后该怎么办