`
talin2010
  • 浏览: 522620 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

17.7.2 Event accessors

阅读更多
[Note: Event declarations typically omit event-accessor-declarations, as in
the Button example above. One
situation for doing so involves the case in which the storage cost of one
field per event is not acceptable. In such
cases, a class can include event-accessor-declarations and use a private
mechanism for storing the list of event
handlers. Similarly, in cases where the handling of an event requires
access to external resources, event accessors
may be used to manage these resources. end note]
The event-accessor-declarations of an event specify the executable
statements associated with adding and
removing event handlers.
The accessor declarations consist of an add-accessor-declaration and a
remove-accessor-declaration. Each
accessor declaration consists of the token add or remove followed by a
block. The block associated with an addaccessor-
declaration specifies the statements to execute when an event handler is
added, and the block associated
with a remove-accessor-declaration specifies the statements to execute when
an event handler is removed.
Each add-accessor-declaration and remove-accessor-declaration corresponds
to a method with a single value
parameter of the event type, and a void return type. The implicit parameter
of an event accessor is named
value. When an event is used in an event assignment, the appropriate event
accessor is used. Specifically, if the
assignment operator is += then the add accessor is used, and if the
assignment operator is ?= then the remove
accessor is used. In either case, the right-hand operand of the assignment
operator is used as the argument to the
event accessor. The block of an add-accessor-declaration or a
remove-accessor-declaration must conform to the
rules for void methods described in §17.5.8. In particular, return
statements in such a block are not permitted
to specify an expression.
Since an event accessor implicitly has a parameter named value, it is a
compile-time error for a local variable or
constant declared in an event accessor to have that name.
C# LANGUAGE SPECIFICATION
250
[Example: In the example
class Control: Component
{
// Unique keys for events
static readonly object mouseDownEventKey = new object();
static readonly object mouseUpEventKey = new object();
// Return event handler associated with key
protected Delegate GetEventHandler(object key) {?}
// Add event handler associated with key
protected void AddEventHandler(object key, Delegate handler) {?}
// Remove event handler associated with key
protected void RemoveEventHandler(object key, Delegate handler) {?}
// MouseDown event
public event MouseEventHandler MouseDown {
add { AddEventHandler(mouseDownEventKey, value); }
remove { RemoveEventHandler(mouseDownEventKey, value); }
}
// MouseUp event
public event MouseEventHandler MouseUp {
add { AddEventHandler(mouseUpEventKey, value); }
remove { RemoveEventHandler(mouseUpEventKey, value); }
}
// Invoke the MouseUp event
protected void OnMouseUp(MouseEventArgs args) {
MouseEventHandler handler;
handler = (MouseEventHandler)GetEventHandler(mouseUpEventKey);
if (handler != null)
handler(this, args);
}
}
the Control class implements an internal storage mechanism for events. The
AddEventHandler method
associates a delegate value with a key, the GetEventHandler method returns
the delegate currently associated
with a key, and the RemoveEventHandler method removes a delegate as an
event handler for the specified
event. Presumably, the underlying storage mechanism is designed such that
there is no cost for associating a
null delegate value with a key, and thus unhandled events consume no
storage. end example]
分享到:
评论

相关推荐

    PyPI 官网下载 | bincopy-17.7.2-py2.py3-none-any.whl

    **PyPI官网下载 | bincopy-17.7.2-py2.py3-none-any.whl** 在Python的世界里,PyPI(Python Package Index)是官方的第三方软件包仓库,它为Python开发者提供了一个发布、查找和安装Python库的平台。`bincopy-...

    node-v17.7.2-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-x64.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-headers.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-headers.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-win-x86.zip

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-win-x64.zip

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-darwin-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-darwin-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-linux-s390x.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-linux-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-darwin-arm64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-darwin-x64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-linux-s390x.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-linux-x64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v17.7.2-linux-arm64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

Global site tag (gtag.js) - Google Analytics