Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking for the last element in a foreach

Tags:

c#

.net

I have this code :

foreach (Object element in elements.under)
    {
        ...
    }

and I'd like to print some only when I'm into the last cycle. How can I do it?

like image 438
markzzz Avatar asked Jun 01 '11 10:06

markzzz


People also ask

How can you tell if an array is last element?

1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.

Is there a forEach in JavaScript?

JavaScript Array forEach()The forEach() method calls a function for each element in an array. The forEach() method is not executed for empty elements.


1 Answers

foreach (Object element in elements.under)
    {
        if (element  == elements.under.Last())  
        {
            //Print Code
        }
        else
        {
            //Do other thing here
        }
    }
like image 92
Rio Stephen Avatar answered Sep 22 '22 15:09

Rio Stephen