c# Foreach last (zz)

c# Foreach last
//z 2012-09-06 15:49:01 IS2120@CSDN.T2414191273[T14,L128,R4,V138]
If you just need to do something with the last element (as opposed to something different with the last element then using LINQ will help here:

Item last = Model.Results.Last();
// do something with last

If you need to so something different with the last element then you'd need something like:

Item last = Model.Results.Last();
foreach (Item result in Model.Results)
{
    // do something with each item
    if (item.Equals(last))
    {
        // do something different with the last item
    }
    else
    {
        // do something different with every item but the last
    }
}

Though you'd probably need to write a custom comparer to ensure that you could tell that the item was the same as the item returned byLast().

            using (System.IO.StreamWriter writer = new System.IO.StreamWriter (@"e:\e.txt"))
            {
                writer.Write(sb.ToString());
            }
posted @ 2012-09-06 15:45  BiG5  阅读(225)  评论(0编辑  收藏  举报