user3112996
user3112996

Reputation: 79

Check specific values in an array

I have 2 arrays which I compare below.

int[] values = { 1, 2, 3, 4, etc };

int[,] limits = { { 1, 2 }, { 2, 5 }, { 2, 6 },etc };

I can compare all the elements of the array quite easily, or specific elements in the array if they are sequential to see if they all fall within the corresponding limits using the following code,

 if (Enumerable.Range(0, values.Length).All(x => values[x] >= limits[x, 0] && values[x] <= limits[x, 1]))
 {
    //Do something            
 };

However, If I wanted to check only specific, non sequentional indecies in the array, eg indexes 0,4,6 & 9 how can I do this ? Thank in advance.

Upvotes: 0

Views: 70

Answers (1)

juharr
juharr

Reputation: 32266

Just replace Enumerable.Range with a collection of the indexes you want to check

new[] {0, 4, 6, 9}.All(x => values[x] >= limits[x, 0] && values[x] <= limits[x, 1])

Upvotes: 5

Related Questions