Razor
Razor

Reputation: 17498

What are the differences between these Array methods

These static Array methods have me puzzled. They seem to do the same things. Are they available for older legacy code?

Array.IndexOf
Array.FindIndex

Array.LastIndexOf
Array.FindLastIndex

Upvotes: 4

Views: 491

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415690

One accepts an item to match. The other other accepts a function that checks an item and return true if matches, false if it does not.

For example:

var x = {1,2,3,4,5,6};
int i = Array.IndexOf(x, 2);
int j = Array.FindIndex(x, a => a == 2);

Upvotes: 3

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

FindIndex takes a predicate.

Two different ways to find 6 in:

var nums = new[]{1,3,7,6,5};

First even:

Array.FindIndex(nums, val=>val % 2 == 0);

Value:

Array.IndexOf(nums, 6);

Upvotes: 0

Related Questions