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

后端 未结 13 1345
离开以前
离开以前 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:47

    Independent of your original question, you will get better performance if you capture references to local variables rather than index into your list multiple times:

    AllIntegerIDs ids = new AllIntegerIDs();
    ids.m_MessageID = (int)IntegerIDsSubstring[IntOffset];
    ids.m_MessageType = (int)IntegerIDsSubstring[IntOffset + 1];
    ids.m_ClassID = (int)IntegerIDsSubstring[IntOffset + 2];
    ids.m_CategoryID = (int)IntegerIDsSubstring[IntOffset + 3];
    ids.m_MessageText = MessageTextSubstring;
    integerList.Add(ids);
    

    And in your for loop:

    for (int cnt3 = 0 ; cnt3 < integerList.Count ; cnt3++) //<----PROBLEM HERE
    {
       AllIntegerIDs ids = integerList[cnt3];
       Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\n",
          ids.m_MessageID,ids.m_MessageType,ids.m_ClassID,ids.m_CategoryID, ids.m_MessageText);
    }
    

提交回复
热议问题