什么是代表?

发布于 2024-09-07 00:57:02 字数 400 浏览 2 评论 0原文

可能的重复:
C# - 有人可以告诉我为什么和我应该在哪里使用代表?
C# 委托真实世界用法

在 stackoverflow 上搜索答案时,许多代码示例使用代表。这些是什么?他们如何运作?何时、为何以及如何使用它们?请给出一些简单的代码示例

Possible Duplicates:
C# - Can someone tell me why and where I should use delegates?
C# Delegates Real World Usage

While searching for answers here on stackoverflow, many of the code examples use Delegates. What are they? How they work? when, why and how to use them? Please give some simple code examples

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋风の叶未落 2024-09-14 00:57:02

Objective-C 委托是简单的抽象模式。 WikiPedia 对此进行了详细描述。

Objective-C delegates are simple abstraction pattern. It's described very on WikiPedia.

下雨或天晴 2024-09-14 00:57:02

在 C# 中,委托不仅仅是一件事:

  • 委托类型是一种类似于具有单个方法的接口的类型。
  • 委托实例可以像方法一样被调用,只不过您可以在运行时附加和分离处理程序,而不是在编译时定义方法体。如果有多个处理程序调用委托,将导致所有处理程序被调用(假设没有抛出异常)。

Jon Skeet 的有关事件和代表的文章更深入地介绍了这一点。

A delegate is more than one thing in C#:

  • A delegate type is a type which similar to an interface with a single method.
  • A delegate instance can be called like a method except that you can attach and detach handlers to it at runtime instead of it having a method body defined at compile time. If there are multiple handlers invoking the delegate will result in all the handlers being called (assuming no exception is thrown).

Jon Skeet's article on events and delegates covers this in more depth.

[浮城] 2024-09-14 00:57:02

代表基本上是一流的职能。

也就是说,它们是在程序中作为自己的实体独立存在的函数,而不是绑定到另一个实体(例如类)。

如果您有 C/C++ 背景,则可以将它们视为与函数指针等效的强类型 .NET。

Delegates are basically first-class functions.

That is, they are functions which stand alone as their own entities in the program, as opposed to being tied to another entity such as a class.

If you have C/C++ background, it is acceptable to think of them as a strongly-typed .NET equivalent to function pointers.

独夜无伴 2024-09-14 00:57:02

委托是一个类型安全的函数指针 - 通常用于实现抽象、回调等。

请参阅以下答案:

C# 中的委托比 C/C++ 中的函数指针更好吗?

委托有哪些优点?

委托和回调是否相同或相似?

A delegate is a typesafe function pointer - typically used to implement abstraction, callback, etc.

See these answers:

How are delegates in C# better than function pointers in C/C++?

What are the advantages of delegates?

Are delegates and callbacks the same or similar?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文