C# - Can someone tell me why and where I should use delegates?(C# - 有人能告诉我为什么以及在哪里应该使用委托吗?)
问题描述
我认为我将 C# 中委托的概念理解为指向方法的指针,但我找不到任何好的示例来说明在哪里使用它们是个好主意.有哪些例子对于代表来说更优雅/更好,或者不能使用其他方法解决?
I think I understand the concept of a delegate in C# as a pointer to a method, but I cant find any good examples of where it would be a good idea to use them. What are some examples that are either significantly more elegant/better with delegates or cant be solved using other methods?
推荐答案
你说的代表到底是什么意思?以下是它们的两种使用方式:
What exactly do you mean by delegates? Here are two ways in which they can be used:
void Foo(Func<int, string> f) {
//do stuff
string s = f(42);
// do more stuff
}
和
void Bar() {
Func<int, string> f = delegate(i) { return i.ToString(); }
//do stuff
string s = f(42);
// do more stuff
}
第二个要点是您可以动态声明新函数,作为委托.这可以在很大程度上被 lambda 表达式所取代,并且在您有一小段逻辑想要 1)传递给另一个函数或 2)只是重复执行时很有用.LINQ 就是一个很好的例子.每个 LINQ 函数都将 lambda 表达式作为其参数,指定行为.例如,如果你有一个 List<int>l
然后 l.Select(x=>(x.ToString())
将对列表中的每个元素调用 ToString().我编写的 lambda 表达式实现为委托.
The point in the second one is that you can declare new functions on the fly, as delegates. This can be largely replaced by lambda expressions,and is useful any time you have a small piece of logic you want to 1) pass to another function, or 2) just execute repeatedly. LINQ is a good example. Every LINQ function takes a lambda expression as its argument, specifying the behavior. For example, if you have a List<int> l
then l.Select(x=>(x.ToString())
will call ToString() on every element in the list. And the lambda expression I wrote is implemented as a delegate.
第一个案例展示了如何实现 Select.您将委托作为参数,然后在需要时调用它.这允许调用者自定义函数的行为.再次以 Select() 为例,函数本身保证了你传递给它的委托会在列表中的每个元素上被调用,并且每个元素的输出都会被返回.该委托实际上做什么取决于您.这使它成为一个非常灵活和通用的功能.
The first case shows how Select might be implemented. You take a delegate as your argument, and then you call it when needed. This allows the caller to customize the behavior of the function. Taking Select() as an example again, the function itself guarantees that the delegate you pass to it will be called on every element in the list, and the output of each will be returned. What that delegate actually does is up to you. That makes it an amazingly flexible and general function.
当然,它们也用于订阅事件.简而言之,委托允许您引用函数,将它们用作函数调用中的参数,将它们分配给变量以及您喜欢做的任何其他事情.
Of course, they're also used for subscribing to events. In a nutshell, delegates allow you to reference functions, using them as argument in function calls, assigning them to variables and whatever else you like to do.
这篇关于C# - 有人能告诉我为什么以及在哪里应该使用委托吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# - 有人能告诉我为什么以及在哪里应该使用委托吗?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01