To find index of a element in array often we use statement for by next way:
static int SearchIndexOfItemInArray_via_for(int[] data, int foundItemValue)C# (2.0) suggests another way to search index of element with help of method Array.FindIndex and delegate:
{
int itemIndex = -1;
for (int i = 0; i < data.Length; i++)
{
if (foundItemValue == data[i])
{
itemIndex = i;
break;
}
}
return itemIndex;
}
static int SearchIndexOfItemInArray_via_FindIndex(int[] data, int foundItemValue)Additional links:
{
return Array.FindIndex(data, delegate(int item)
{
return item == foundItemValue;
});
}
Array..::.FindIndex<(Of <(T>)>) Generic Method (array<T>[]()[], Predicate<(Of <(T>)>))
1 comment:
Nice example and counter-example. Much easier to follow than say, the MSDN page.
Post a Comment