How can I find the last element in a List<>?

后端 未结 13 1313
离开以前
离开以前 2020-12-12 21:43

The following is an extract from my code:

public class AllIntegerIDs 
{
    public AllIntegerIDs() 
    {            
        m_MessageID = 0;
        m_Messa         


        
13条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 21:50

    To get the last item of a collection use LastOrDefault() and Last() extension methods

    var lastItem = integerList.LastOrDefault();
    

    OR

    var lastItem = integerList.Last();
    

    Remeber to add using System.Linq;, or this method won't be available.

提交回复
热议问题