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

后端 未结 13 1352
离开以前
离开以前 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 22:09

    Though this was posted 11 years ago, I'm sure the right number of answers is one more than there are!

    You can also doing something like;

    
    if (integerList.Count > 0) 
       var item = integerList[^1];
    
    

    See the tutorial post on the MS C# docs here from a few months back.

    I would personally still stick with LastOrDefault() / Last() but thought I'd share this.

    EDIT; Just realised another answer has mentioned this with another doc link.

提交回复
热议问题