checking for the last element in a foreach
foreach (Object element in elements.under)
{
if (element == elements.under.Last())
{
//Print Code
}
else
{
//Do other thing here
}
}
You need to keep track of a counter and then check for last element -
int i = 1;
foreach (Object element in elements.under)
{
if (i == elements.under.Count) //Use count or length as supported by your collection
{
//last element
}
else
{ i++; }
}
this answer on so may resolve the issue you want have look to it
How do you find the last loop in a For Each (VB.NET)?
also check this library Enumerating with extra info in the Miscellaneous Utility Library
foreach (SmartEnumerable<string>.Entry entry in
new SmartEnumerable<string>(list))
{
Console.WriteLine ("{0,-7} {1} ({2}) {3}",
entry.IsLast ? "Last ->" : "",
entry.Value,
entry.Index,
entry.IsFirst ? "<- First" : "");
}
code based on above library link