Array.Find与更多参数 [英] Array.Find with more parameters

查看:71
本文介绍了Array.Find与更多参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我发现在C#中使用Array.Find非常有用.我知道它的工作方式类似于以下示例:

Hey everybody,

I find that using Array.Find is very useful in C#. I know it works like the following example:

public void Main
{
String[] names = new String[4]     
{"john","mary","peter","justin");
String theone = Array.Find(names,MyCondition);
}

private Boolean MyCondition(String name)
{
return name.Lenght==5; //supposing that I want a name just with 5 letters
}



但是我想知道是否可以向"MyCondition"发送更多参数(例如,我要查找的长度).我尝试了一下,Visual Studio生成了这样的新方法:



But I was wondering if I can send more parameters to ''MyCondition'' (for example the lenght I am looking for). I tryed it and Visual Studio generated a new method like this:

public void Main
{
String[] names = new String[5]     
{"john","mary","peter","justin");
int lenght = 5;
String theone = Array.Find(names,MyCondition(names, lenght));
}
private Predicate<String> MyCondition(String name, int lenght)
{
}


而且我不知道从这里做什么:〜
您能帮助我如何使用以前从未见过的这种新方法吗?或者,如果我想发送更多的参数给谓词而不是默认方法,该怎么办?


And I don''t know what to do from here :~
Can you help me please how to use this new method I''ve never seen before? Or, What can I do if I want to send more parameters to a predicate than just one as a default method?

推荐答案

您可以使用闭包机制和匿名机制函数,您将不必传递任何内容,因为匿名函数可以看到任何内容.看到这里:

You can use the closure mechanism and an anonymous function and you won''t have to pass anything in as it will be visible to the anonymous function. See here:

public void Main
{
    String[] names = new String[5]{"john","mary","peter","justin");
    int length = 5;
    String theone = Array.Find(names,delegate(String name)
                                     {
                                          return name.Length == length;
                                     }
    );

}



Array.Find采用一个数组和一个布尔委托(String x)方法.这意味着一个方法将只接受一个String参数并返回bool.如果您需要传递字符串以外的其他内容,则也可以使用通用形式.长度将不必作为参数传递,因为它对于匿名方法是可见的.

还有问题吗?发表评论.

干杯!



Array.Find takes an array and a bool delegate(String x) method. That means a method that takes exactly one String parameter and returns bool. You can also use the Generic form if you need to pass something other than a String. length will not have to be passed as a parameter since it is visible to the anonymous method.

Anymore questions? Leave a comment.

Cheers!


您可以将string []数组传递到通用列表构造函数中,并使用带有lambda表达式的列表的.Find()方法,该表达式可以根据需要检查尽可能多的条件. :

You could pass your string[] array into a generic list constructor and use the .Find() method of the list with a lambda expression that checks for as many conditions as you please:

public void Main
{
List<string> names = new List<string>(new String[4]{"john","mary","peter","justin"));

int length = 5;

String theone = names.Find(s => (s == "john" && s.Length == length));
}
</string></string>



您也许可以将类似的lambda表达式与Array.Find()一起使用,但我不知道,因为我从不使用Array方法.



You might be able to use a similar lambda expression with Array.Find(), but I don''t know because I never use Array methods.


这可能看起来有些不可思议.最初,但这最终将完成这项工作:
This may look like a bit of magic initially, yet this is what will ultimately do the job:
public void Main() {
    var names = new[] { "john", "mary", "peter", "justin" };
    var lenght = 5;
    var theone = Array.Find(names, MyCondition(lenght));
}
private static Predicate<string> MyCondition(int lenght) {
    return s => s.Length == lenght;
}

了解这里发生的事情的最好方法是在"return"语句的行上设置一个断点,然后在调试器中运行程序.当您第二次到达断点时,打开调用堆栈.

The best way to understand what''s going on here is to set a breakpoint on the line with the "return" statement, and run the program in the debugger. When you reach the breakpoint for the second time, open the call stack.


这篇关于Array.Find与更多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆