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.
attributesopt delegate-modifiersopt delegate return-type identifier (
formal-parameter-listopt ) ;
delegate-modifiers delegate-modifier
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
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
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
? 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:
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]

Forward Declarations

Forward declarations are a great way to eliminate needless compile-time dependencies. But heres an e...
  • team2vx
  • team2vx
  • 2007年11月24日 20:06
  • 981

flex4 -- fx:Declarations

fx下放置了顶级包的内容。如数据类型、全局函数和全局常量等。 在flex3里这样写是合法的:,但在Gumbo中却是不行的,正确做法需要用到来申明,比如: 100 ...
  • ld_flex
  • ld_flex
  • 2012年03月14日 17:41
  • 3631

ISO C89 forbids mixed declarations and code

ISO C90 forbids mixed declarations and code     应在写任何代码之前先进行声明。不要将声明和代码混合在一起。...
  • nemo2011
  • nemo2011
  • 2012年03月17日 10:28
  • 1337

dhcpd 一个警告信息的解决方法

dhcp配置中经常会看到如下的警告信息:dhcpd: WARNING: Host declarations are global.  They are not limited to the scope...
  • lhfeng
  • lhfeng
  • 2010年10月29日 16:40
  • 2647

linux gcc编译错误:‘for’ loop initial declarations are only allowed in C99 mode

linux系统下的c编程与windows有所不同,如果你在用gcc编译代码的时候提示‘for’ loop initial declarations are only allowed in C99 ...
  • zhangmuyan
  • zhangmuyan
  • 2012年04月09日 19:35
  • 6220

Variable Declarations

Scala allows you to decide whether a variable is immutable (read-only) or not (readwrite) when you d...
  • qq_16912257
  • qq_16912257
  • 2016年10月05日 17:08
  • 163

ISO C90 forbids mixed declarations and code[-Wdeclaration-after-statement]

ISO C90 forbids mixed declarations and code[-Wdeclaration-after-statement]
  • yanlaifan
  • yanlaifan
  • 2016年04月23日 13:01
  • 2485

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside stric

我的是 node的版本太低造成的,我去官网上下载了个最新的版本,就没有这个问题了。 https://nodejs.org/zh-cn/download/current/...
  • qwdafedv
  • qwdafedv
  • 2017年02月27日 12:04
  • 2837

编译器问题,error: 'for' loop initial declarations are only allowed in C99 mode

使用gcc编译代码是报出 error: 'for' loop initial declarations are only allowed in C99 mode note: use opt...
  • guofu8241260
  • guofu8241260
  • 2014年04月09日 09:36
  • 3361


目录目录 架构概览 模块 组件 模板 元数据Metadata 数据绑定 指令directive 服务 依赖注入 其他重要的Angular特性和服务架构概览 用 Angular 扩展语法编写 HTML ...
  • qq_15096707
  • qq_15096707
  • 2016年12月25日 18:16
  • 1329
您举报文章:22.1 Delegate declarations