Tuesday, April 7, 2009

C# - List.FindIndex Method

Searches for an element that matches the conditions
defined by the specified predicate, and returns the
zero-based index of the first occurrence within the
entire List<(Of <(T>)>).


List<T>.FindIndex Method (Predicate<T>)

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

public int FindIndex(
Predicate<T> match
)



List<string> myLst = new List<string>();
...
// fill the list
...
int iIndx = 0;
if ( (iIndx = myLst.FindIndex(iIndx, StartWithEqualSign)) != -1 )
{
// Code for the row in the list which you found
}


// Search predicate returns true if a string starts with "=".
private static bool StartWithEqualSign(String s)
{
bool retVal = false;
if ( (s.Length > 0) && (s.Substring(0, 1) == "=") )
retVal = true;
return retVal;
}

Share/Bookmark

No comments: