C# 功能说明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/878650/
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
Explanation of Func
提问by zSynopsis
I was wondering if someone could explain what Func<int, string>
is and how it is used with some clear examples.
我想知道是否有人可以Func<int, string>
通过一些清晰的例子来解释它是什么以及它是如何使用的。
采纳答案by Jon Skeet
Are you familiar with delegates in general? I have a page about delegates and eventswhich may help if not, although it's more geared towards explaining the differences between the two.
你对一般的代表熟悉吗?我有一个关于代表和事件的页面,如果没有,它可能会有所帮助,尽管它更倾向于解释两者之间的差异。
Func<T, TResult>
is just a generic delegate - work out what it means in any particular situation by replacing the type parameters(T
and TResult
) with the corresponding type arguments(int
and string
) in the declaration. I've also renamed it to avoid confusion:
Func<T, TResult>
只是一个通用委托 - 通过在声明中用相应的类型参数(和)替换类型参数(T
和TResult
)来找出它在任何特定情况下的含义。我还重命名了它以避免混淆:int
string
string ExpandedFunc(int x)
In other words, Func<int, string>
is a delegate which represents a function taking an int
argument and returning a string
.
换句话说,Func<int, string>
是一个代表一个函数的委托,它接受一个int
参数并返回一个string
。
Func<T, TResult>
is often used in LINQ, both for projections and predicates (in the latter case, TResult
is always bool
). For example, you could use a Func<int, string>
to project a sequence of integers into a sequence of strings. Lambda expressionsare usually used in LINQ to create the relevant delegates:
Func<T, TResult>
在 LINQ 中经常使用,用于预测和谓词(在后一种情况下,TResult
总是bool
)。例如,您可以使用 aFunc<int, string>
将整数序列投影到字符串序列中。Lambda 表达式通常在 LINQ 中用于创建相关委托:
Func<int, string> projection = x => "Value=" + x;
int[] values = { 3, 7, 10 };
var strings = values.Select(projection);
foreach (string s in strings)
{
Console.WriteLine(s);
}
Result:
结果:
Value=3
Value=7
Value=10
回答by Andrew Hare
It is a delegate that takes one int
as a parameter and returns a value of type string
.
它是一个委托,它将一个int
参数作为参数并返回一个类型为 的值string
。
Here is an example of its usage:
下面是它的用法示例:
using System;
class Program
{
static void Main()
{
Func<Int32, String> func = bar;
// now I have a delegate which
// I can invoke or pass to other
// methods.
func(1);
}
static String bar(Int32 value)
{
return value.ToString();
}
}
回答by JP Alioto
A Func<int, string>
eats ints and returns strings. So, what eats ints and returns strings? How about this ...
AFunc<int, string>
吃整数并返回字符串。那么,什么吃整数并返回字符串?这个怎么样 ...
public string IntAsString( int i )
{
return i.ToString();
}
There, I just made up a function that eats ints and returns strings. How would I use it?
在那里,我刚刚编写了一个吃整数并返回字符串的函数。我将如何使用它?
var lst = new List<int>() { 1, 2, 3, 4, 5 };
string str = String.Empty;
foreach( int i in lst )
{
str += IntAsString(i);
}
// str will be "12345"
Not very sexy, I know, but that's the simple idea that a lot of tricks are based upon. Now, let's use a Func instead.
我知道不是很性感,但这是很多技巧都基于的简单想法。现在,让我们改用 Func。
Func<int, string> fnc = IntAsString;
foreach (int i in lst)
{
str += fnc(i);
}
// str will be "1234512345" assuming we have same str as before
Instead of calling IntAsString on each member, I created a reference to it called fnc (these references to methods are called delegates) and used that instead. (Remember fnc eats ints and returns strings).
我没有在每个成员上调用 IntAsString,而是创建了一个对它的引用,称为 fnc (这些对方法的引用称为delegates)并改为使用它。(记住 fnc 吃整数并返回字符串)。
This example is not very sexy, but a ton of the clever stuff you will see is based on the simple idea of functions, delegates and extension methods.
这个例子不是很吸引人,但是你会看到的很多聪明的东西都是基于函数、委托和扩展方法的简单概念。
One of the best primers on this stuff I've seen is here. He's got a lot more real examples. :)
我见过的关于这些东西的最好的入门书之一是这里。他有更多真实的例子。:)
回答by jts
Func<int, string>
accepts an int value parameter and returns a string value. Here's an example where an additional supporting method is unnecessary.
Func<int, string>
接受一个 int 值参数并返回一个字符串值。这是一个不需要额外支持方法的示例。
Func<int, string> GetDogMessage = dogAge =>
{
if (dogAge < 3) return "You have a puppy!";
if (dogAge < 7) return "Strong adult dog!";
return "Age is catching up with the dog!";
};
string youngDogMessage = GetDogMessage(2);
NOTE: The last object type in Func (i.e. "string" in this example) is the functions return type (i.e. not limited to primitives, but any object). Therefore, Func<int, bool, float>
accepts int and bool value parameters, and returns a float value.
注意:Func 中的最后一个对象类型(即本例中的“字符串”)是函数返回类型(即不限于基元,而是任何对象)。因此,Func<int, bool, float>
接受 int 和 bool 值参数,并返回一个浮点值。
Func<int, bool, float> WorthlessFunc = (intValue, boolValue) =>
{
if(intValue > 100 && boolValue) return 100;
return 1;
};
float willReturn1 = WorthlessFunc(21, false);
float willReturn100 = WorthlessFunc(1000, true);
HTH
HTH