C# 中的标准委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/729912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Standard delegates in C#
提问by Tarion
There are some Delegates predefined in C#
C#中预定义了一些委托
I know these:
我知道这些:
EventHandler // Default event callbacks
EventHandler<T> // Default event callbacks with custom parameter (inheriting from EventArgs)
Action // Function without return value and without parameter
Action<T1, T2, T3, T4> // Function without return value and 1-4 parameters
Func<T1, T2, T3, T4, TResult> // Methos with 0-4 parameters and one result type
Predicate<T> // equivalent to Func<T, bool>
There are many more for special cases and generated form parts of the framework, but these are often good to use in self written code.
还有更多用于框架的特殊情况和生成的表单部分,但这些通常适用于自己编写的代码。
If you know some more useful add them. Otherwise this is answered.
如果你知道一些更有用的添加它们。否则这是回答。
采纳答案by Jon Skeet
They're not predefined in C#. They're defined by the framework.
它们不是在 C# 中预定义的。它们由framework定义。
The Action
and Func
delegate families are wider than you've shown - they go up to
在Action
和Func
比你已经证明委托家庭是更广泛的-他们上去
Action<T1, T2, T3, T4>
and
和
Func<T1, T2, T3, T4, TResult>
Another common-ish one in .NET 2.0 for list manipulation (before LINQ) is Predicate<T>
.
.NET 2.0 中另一个常见的列表操作(在 LINQ 之前)是Predicate<T>
.
For working with threads:
用于处理线程:
ThreadStart
ParameterizedThreadStart
WaitCallback
TimerCallback
AsyncCallback
回答by NileshChauhan
回答by Brian Genisio
I like to use Predicate<T>
which is equivalent to Func<T, bool>
我喜欢使用Predicate<T>
它相当于Func<T, bool>
回答by Kent Boogaart
I use WaitCallback
and ThreadStart
often enough for them to get a mention.
我经常使用WaitCallback
并且ThreadStart
足以让他们提及。
If you know the signatureof the delegate you're after, but you don't know if there's an existingdelegate with that signature that you can use, you can follow these instructionson my blog to find one.
如果您知道签名你后委托的,但你不知道,如果有一个现有的与签名的委托,您可以使用,你可以按照这些指令在我的博客找到一个。
回答by CSharper
System.Windows.ValidateValueCallback which represents a method used as a callback that validates the effective value of a dependency property.
System.Windows.ValidateValueCallback 表示用作回调的方法,用于验证依赖项属性的有效值。
Read More: MSDN: ValidateValueCallback Delegate
回答by Derek Davidson PST CST
I've also come across
我也遇到过
Comparison<T>
in the
在里面
List.Sort(Comparison<T>)
generic method. I suppose this is now equivalent to:
通用方法。我想这现在相当于:
Func<T1, T2, int>