22.1 Delegate declarations

原创 2006年06月22日 03:19:00
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-modifiers 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 within another
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
delegate, and return-type indicates the return
type of the delegate. A method and a delegate type are compatible if both
of the following are true:
? They have the same number or parameters, with the same types, in the same
order, with the same parameter
modifiers.
? Their return-types are the same.
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 delegate 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, these 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
they 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 that 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
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.Delegate type
via the usual member access syntax.
The set of methods encapsulated by a delegate instance is called 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 the
order 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. When
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]
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

<Effective Mordern C++>笔记:Item 9:prefer alias declarations to typedefs.

转载自原博:原博 C++11中引入的std::unique_ptr智能指针是个好用的东西,在我们使用unique_ptr的时候往往会写出这样的类型std::uniqeu_ptr<std::unorde...

学习笔记一:命名空间(namespace)之四:using declarations和using directives的区别

(本文主要参考c++ primer第17章2.4节内容。) 由前面内容,我们知道c++引入命名空间(namespace)概念的主要目的是避免命名冲突。但是当我们想要引用命名空间成员时,我们就会体会到...

算法导论-第22章-基本的图算法-22.1 图的表示

一、综述 图的表示方法通常有两种,即邻接表表示法和邻接矩阵表示法。这两种方法都可以表示有向图和无向图 1.邻接表表示法 (1)用邻接表表示无向图 (2)用邻接表表示有向...

Return type declarations返回类型声明

PHP 7.新增了返回类型声明 http://php.net/manual/en/functions.returning-values.php 在PHP 7.1中新增了返回类型声明为void,以及...

算法导论22.1-6

找到一个矩阵表示法的有向无权图中的"汇",定义为,入度为|V| - 1,出度为0的顶点.这样的顶点,一个图中只能有一个.或者没有.这个问题,是以前没有接触过的,觉得很有用,所以去写了下.   起初写...

Swift 声明(Declarations)

一条声明(declaration)可以在程序里引入新的名字或者构造。举例来说,可以使用声明来引入函数和方法,变量和常量,或者来定义新的命名好的枚举,结构,类和协议类型。可以使用一条声明来延长一个已经存...

《算法导论》习题解答 Chapter 22.1-5(求平方图)

一、邻接矩阵实现 思路:如果是邻接矩阵存储,设邻接矩阵为A,则A*A即为平方图,只需要矩阵相乘即可; 伪代码: for i=1 to n for j=1 to n for k=1 ...
  • hcbbt
  • hcbbt
  • 2013-07-16 01:19
  • 2441

Item5 Prefer auto to explicit type declarations

这个系列的文章来自于Effective Modern C++的读书笔记,我抽取了其中比较重要的,不容易理解的,平常我们开发过程中也不太在意的一些Item进行分析。 写C/C++的程序员都知道定义一个变...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)