What I am trying to do is loop through an integer array
int[] integerarray = { 1, 2, 3, 4 };
for (??????)
{
// What do I do here?
}
Until I get to 3. I'm not sure how though.
What I am trying to do is loop through an integer array
int[] integerarray = { 1, 2, 3, 4 };
for (??????)
{
// What do I do here?
}
Until I get to 3. I'm not sure how though.
we can achieve this by using simple for each loop
foreach(int i in integerarray)
{
if(i==3)
{
// do your stuf here;
break;
}
}
You can use linq TakeWhile method to get the elements from a sequence as long as a specified condition is true.
Here you want that return element untill we found 3 in the sequence so you can write this statement like this
var result = integerarray.TakeWhile(x => !x.Equals(3)).ToList();
result will contains the items which comes before 3
in your case result will have 1,2
Use LINQ.
int[] integerarray = { 1, 2, 3, 4 };
for (var number = integerarray.TakeWhile(x => x != 3))
{
// Do something
}
{ 4, 2, 3, 1 }; ?