转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件使用 >> 系统软件 >> 正文
14.5.10.3 Delegate creation expressions         

14.5.10.3 Delegate creation expressions

作者:闵涛 文章来源:闵涛的学习笔记 点击数:878 更新时间:2009/4/25 0:44:46
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]


[系统软件]14.5.10.1 Object creation expressions  [系统软件]14.5.10.2 Array creation expressions
[VB.NET程序]用JScript实现VB.Net,C#的[委托Delegate]:  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · 办公软件  · 系统软件
    · 常用软件  · 聊天工具
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台