Problem
An example of my code can be found below:
public class AllIntegerIDs
{
public AllIntegerIDs()
{
m_MessageID = 0;
m_MessageType = 0;
m_ClassID = 0;
m_CategoryID = 0;
m_MessageText = null;
}
~AllIntegerIDs()
{
}
public void SetIntegerValues (int messageID, int messagetype,
int classID, int categoryID)
{
this.m_MessageID = messageID;
this.m_MessageType = messagetype;
this.m_ClassID = classID;
this.m_CategoryID = categoryID;
}
public string m_MessageText;
public int m_MessageID;
public int m_MessageType;
public int m_ClassID;
public int m_CategoryID;
}
In my main() function code, I’m attempting to use the following:
List<AllIntegerIDs> integerList = new List<AllIntegerIDs>();
/* some code here that is ised for following assignments*/
{
integerList.Add(new AllIntegerIDs());
index++;
integerList[index].m_MessageID = (int)IntegerIDsSubstring[IntOffset];
integerList[index].m_MessageType = (int)IntegerIDsSubstring[IntOffset + 1];
integerList[index].m_ClassID = (int)IntegerIDsSubstring[IntOffset + 2];
integerList[index].m_CategoryID = (int)IntegerIDsSubstring[IntOffset + 3];
integerList[index].m_MessageText = MessageTextSubstring;
}
The issue is that I’m trying to use a for loop to print all of the components in my List:
for (int cnt3 = 0 ; cnt3 <= integerList.FindLastIndex ; cnt3++) //<----PROBLEM HERE
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\n", integerList[cnt3].m_MessageID,integerList[cnt3].m_MessageType,integerList[cnt3].m_ClassID,integerList[cnt3].m_CategoryID, integerList[cnt3].m_MessageText);
}
I’m looking for the last element so that I may use cnt3 in my for loop and print out all of the List’s contents. As noted in the code sample, each entry in the list is an object of the class AllIntegerIDs. How do I locate the List’s last valid entry?
Is it a good idea to utilize anything like integerList? If null, find (integerList[].m MessageText == null;?
If I do that, I’ll need an index that goes from 0 to whatever the maximum is. That means I’ll have to use a different for loop, which I don’t want to do. Is there a faster/better option?
Asked by zack
Solution #1
Use the LastOrDefault() and Last() extension methods to get the last item in a collection.
var lastItem = integerList.LastOrDefault();
OR
var lastItem = integerList.Last();
Remember to use System.Linq; otherwise this method will be unavailable.
Answered by Kundan Singh Chouhan
Solution #2
You can do this if you merely want to get to the last item on the list.
if (integerList.Count > 0)
{
// pre C#8.0 : var item = integerList[integerList.Count - 1];
// C#8.0 :
var item = integerList[^1];
}
The Count property can be used to get the total number of items in a list.
var itemCount = integerList.Count;
Answered by Jared
Solution #3
You can receive the last item with a detailed explanation of the operator in C# 8.0.
List<char> list = ...;
var value = list[^1];
// Gets translated to
var value = list[list.Count - 1];
Answered by Alex Peng
Solution #4
Let’s get to the heart of the matter: how to safely address the last element of a List…
Assuming
List<string> myList = new List<string>();
Then
//NOT safe on an empty list!
string myString = myList[myList.Count -1];
//equivalent to the above line when Count is 0, bad index
string otherString = myList[-1];
Counting to one is a dangerous habit unless you make sure the list isn’t empty first.
There is no other way to avoid testing for an empty list but to do so.
The quickest method that comes to mind is
string myString = (myList.Count != 0) ? myList [ myList.Count-1 ] : "";
You could go full out and create a delegate that always returns true, then send it to FindLast to get the most recent value (or default constructed valye if the list is empty). Despite the fact that the technique is generally O(1), this function starts at the end of the list and will be Big O(1) or constant time (n).
//somewhere in your codebase, a strange delegate is defined
private static bool alwaysTrue(string in)
{
return true;
}
//Wherever you are working with the list
string myString = myList.FindLast(alwaysTrue);
If you count the delegate part, the FindLast method is ugly, but it only needs to be stated once. If the list is empty, a default created value of the list type “” for string will be returned. It would be more useful to take the alwaysTrue delegate a step further and make it a template rather than a string type.
Answered by JFDev
Solution #5
int lastInt = integerList[integerList.Count-1];
Answered by Dan Diplo
Post is based on https://stackoverflow.com/questions/1246918/how-can-i-find-the-last-element-in-a-list