Using Find Method in Generic Controls With C# – T List.Find( item)
Yazan: esersahin 20/10/2008
http://www.aspnetsource.com/Blog/Article.aspx?articleId=9a5fe1f6-6114-44ae-8388-8736d34e9172
Using Find Method in Generic Controls With C# – T List<T>.Find(<T> item)
Well, it’s little strange to us that in class System.Collections.Generic.List<T> there’s no overloads for method Find with parameter <T> item (only public T Find(Predicate<T> match)), so if we have:
List<T> list and want to search for T item(local variable) in list, we can do that:
List.Find(delegate (T t) { return item == t; } );
Follows example in C# for List<string>:
List<string> list = new List<string>(3);
list.Add(“C++”);
list.Add(“C#”);
list.Add(“VB.NET”);
// search for ‘java’ in list
string result = list.Find(delegate(string s) { return s.Equals(“java”); });
// if ‘java’ is not found message box is displayed
if (result == null)
Console.WriteLine(“java hasn’t been found!”);
// search for ‘C#’ in list
result = list.Find(delegate(string s) { return s.Equals(“C#”); });
// if ‘C#’ is found message box is displayed
if (result != null)
Console.WriteLine(“C# has been found!”);