Get and thenDelete item in top of list
I need to get and then delete the item in top of list. I think it's easy but I don't know the commands of list and couldn't find.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sergio FonsecaPosted Mar 2, 2008, 8:30 AM
AlanPosted Mar 2, 2008, 5:57 AM
If by 'top' you mean the first item in the List, then try this short program:
using System;
using System.Collections.Generic;
class Test names = new List();
{
static void Main()
{
List
names.Add("Sergio");
names.Add("Mark");
names.Add("David");
names.Add("Alan");
Console.Clear();
Console.WriteLine("Original list :\n");
foreach(string name in names)
{
Console.WriteLine(name);
}
// Get first item, display and delete it
string first = names[0];
Console.WriteLine("\nFirst name is {0}", first);
names.RemoveAt(0);
Console.WriteLine("\nRevised list :\n");
foreach(string name in names)
{
Console.WriteLine(name);
}
Console.ReadKey();
}
}
To get and delete the last item in the List replace 0 in the names indexer and RemoveAt() method with names.Count - 1.