Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hey everybody,

I find that using Array.Find is very useful in C#. I know it works like the following example:
C#
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
}


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:
C#
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?
Posted

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:

C#
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 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!
 
Share this answer
 
v2
Comments
Sandeep Mewara 2-Feb-11 12:48pm    
Great! 5+
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:

C#
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>


You might be able to use a similar lambda expression with Array.Find(), but I don't know because I never use Array methods.
 
Share this answer
 
Comments
Sandeep Mewara 2-Feb-11 12:48pm    
Another good way! 5+
Manfred Rudolf Bihy 2-Feb-11 18:52pm    
You get my 5 eventhough I must remark that "john" does not have a length of 5 and thus your Find call will return nothing. :)
jim lahey 3-Feb-11 3:29am    
I guess "john" isn't "the one" after all :)
This may look like a bit of magic initially, yet this is what will ultimately do the job:
C#
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;
}
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.
 
Share this answer
 
Comments
Sandeep Mewara 2-Feb-11 12:52pm    
Great! 5+
Manfred Rudolf Bihy 2-Feb-11 18:49pm    
Returning an anonymous method from a method, nice idea! 5+
I still prefer to define these lambdas inline though. The creation from within a method does make sense though for two reasons:
1. Encapsulation of the closure in local/parameter variables of the method
2. Reuse of the returned lambda is possile
dasblinkenlight 2-Feb-11 18:55pm    
I just wanted to stay with the code template from the question as much as possible :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900