[英]Passing parameter to a method and assigning to type Func<bool>
I currently have a method like that resembles this我目前有一个类似的方法
public static bool test(string str);
I would like to assign this method to this type我想将此方法分配给这种类型
Func<bool> callback
I am trying to do this (which is incorrect)我正在尝试这样做(这是不正确的)
callback = test("Str");
The above is incorrect as C# things i am calling a method.以上是不正确的,因为 C# 我正在调用一个方法。 How can I tell it to call that method with the parameter Str?
如何告诉它使用参数 Str 调用该方法? in C++ we can do this
在 C++ 我们可以做到这一点
std::function<...,..,"str">
How do we do something similar in C#?我们如何在 C# 中做类似的事情?
If your goal is to invoke the callback always using the same string argument (and not a different argument each time), you can declare it like:如果您的目标是始终使用相同的字符串参数(而不是每次都使用不同的参数)调用回调,您可以将其声明为:
Func<bool> callback = () => test("Str");
var result = callback();
But if you are intending to pass a different string value each time, then you need a Func<string, bool>
:但是,如果您打算每次传递不同的字符串值,那么您需要一个
Func<string, bool>
:
Func<string, bool> callback = s => test(s);
var result = callback("Str");
you should declare the function as:您应该将 function 声明为:
Func<string, bool> callback
to indicate the method it references consumes a string
and returns a bool
.指示它引用的方法使用一个
string
并返回一个bool
。
then at a later point you can do:然后稍后您可以执行以下操作:
callback = s => test(s);
or inline:或内联:
Func<string, bool> callback = s => test(s);
Let me see if I know what you're getting at.让我看看我是否知道你在说什么。 In C++ we have 'function pointers' and they are declared with a particular signature.
在 C++ 中,我们有“函数指针”,它们用特定的签名声明。 To make the equivalent of that in C# use the delegate keyword.
要使 C# 中的等效项使用委托关键字。
Here's a quick functional code example:这是一个快速的功能代码示例:
class DelegateExample
{
// A delegate is a prototype for a function signature.
// Similar to a function pointer in C++.
delegate bool MyDelegate(string[] strings);
// A method that has that signature.
bool ListStrings(string[] strings)
{
foreach (var item in strings)
{
Console.WriteLine(item);
}
return strings.Length > 0; // ... for example
}
// Different method, same signature.
bool Join(string[] strings)
{
Console.WriteLine(string.Join(", ", strings));
return strings.Length > 0; // ... for example
}
public void TryIt()
{
string[] testData = { "Apple", "Orange", "Grape" };
// Think of this as a list of function pointers...
List<MyDelegate> functions = new List<MyDelegate>();
functions.Add(ListStrings); // This one points to the ListStrings method
functions.Add(Join); // This one points to the Join method
foreach (var function in functions)
{
bool returnVal = function(testData);
Console.WriteLine("The method returned " + returnVal + Environment.NewLine);
}
}
}
You can run the sample in a console app like so:您可以像这样在控制台应用程序中运行示例:
class Program
{
static void Main(string[] args)
{
new DelegateExample().TryIt();
Console.ReadKey();
}
}
Which gives this output:这给出了这个 output:
Apple
Orange
Grape
The method returned True
Apple, Orange, Grape
The method returned True
Hope this is helpful!希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.