Delegate as a function

The function below works fine, but I want to make it simple.

if (list.Exists(delegate(string s) { return s.Contains(str); })) { string name = list.Find(delegate(string s) { return s.Contains(str); }); } 

I am using delegate(string s) { return s.Contains(str); } delegate(string s) { return s.Contains(str); } two times. Is there any way to make this simple. I know how to create a delegate, but I don’t know how to use it.

  //create delegate public delegate bool nameExistsDelegate(List<string> list, string name); // Create a method for a delegate. public static bool IsnameExists(List<string> list, string name) { return list.Exists(delegate(string s) { return s.Contains(name) ; }); } // Create a method for a delegate. public static string GetName(List<string> list, string name) { return list.Find(delegate(string s) { return s.Contains(name) ; }); } 

UPDATE

stuck with .NET 2.0 so i cant use LINQ

+4
source share
3 answers

The anonymous method you use will be converted to a Predicate<string> delegate by the compiler. With this in mind, you can enter local to get rid of unnecessary redundancy.

 Predicate<string> containsStr = delegate(string s) { return s.Contains(str); }; if (list.Exists(containsStr)) { string name = list.Find(containsStr); ... } 

In C # 3.0 or later, you can express it even more severely with lambda expressions.

 Predicate<string> containsStr = s => s.Contains(str); 

In another note, you do not need to check first whether str exists and then continue the search (if the list does not contain zeros), you can simply:

 string name = list.Find(s => s.Contains(str)); if(name != null) { //found } 

Of course, I should also indicate that the strings do not contain extra metadata other than the characters present in them, so you don’t get anything by "finding" a string in the list, just checking if it exists (if you did not mean FindIndex ).

+3
source

if you are on .net 3.5 you can use lamdas

  //create delegate public delegate bool nameExistsDelegate(List<string> list, string name); static Func<string, bool> exists = s => return s.Contains(name); // Create a method for a delegate. public static bool IsnameExists(List<string> list, string name) { return list.Exists(s => exists(s)); } // Create a method for a delegate. public static string GetName(List<string> list, string name) { return list.Find(s => exists(s)); } 
+1
source

I would recommend reading standard delegate types in C #

Here you really need a Predicate that takes an object, checks it with some condition, and returns a pass / fail result.

 Predicate<string> containsCheck = item = > item.Contains(str); if (list.Exists(containsCheck) { string name = list.Find(containsCheck); } 

Note: all code can also be used with LINQ, which is much simpler. But I think you should study delegates right now. Jfyi

 using System.Linq; ... Predicate<string> substringCheck = item => item.Contains(str); var exists = list.Any(substringCheck); var getMatch = list.First(substringCheck); 
0
source

All Articles