score:4
you can use list.remove
, list.removerange
or list.removeat
methods to remove elements from the list.
to remove the last element of the list after you obtained a reference to it using the list indexer you use:
var element = mylist[mylist.count - 1];
mylist.removeat(mylist.count - 1);
using removeat
instead of remove
is more efficient because there is no need to first find the index of the item to be removed and also to note that remove
just removes the first occurrence that matches the item to be removed, so in a list with duplicates it will not be the last element.
finally, if you have an algorithm where will be removing the last item of a list several times, you might as well consider another data structure, like for example a queue
.
score:1
why not use a stack instead of a list it seems more appropriate
[test]
public void mytest()
{
stack<int> stack = new stack<int>(new[] { 1, 2, 3, 4, 5 });
int pop = stack.pop();
assert.areequal(stack.count, 4);
assert.areequal(pop, 5);
}
score:2
create an extension method to do this
public static class extension
{
public static string removelast(this ilist<string> mylist)
{
int lastitemindex = mylist.count - 1;
string lastitem = mylist.last();
mylist.removeat(lastitemindex);
return lastitem;
}
}
call it as
string itemremoved = lst.removelast(); //this will delete the last element from list & return last element as well
score:4
you can create an extension method that takes any index to do the fetch/removal:
public static t extract<t>(this list<t> list, int index)
{
var item = list[index];
list.removeat(index);
return item;
}
the last item can always be found using count - 1
, so the call is simply:
var item = list.extract(list.count - 1);
it then means you can "extract" any item by index.
score:5
you can do as following if you want to take the last.
string lastitem = mylist.last();
mylist.remove(mylist.last());
//mylist.count() will now be 4.
Source: stackoverflow.com
Related Query
- c# Linq or code to extract groups from a single list of source data
- How can I extract a list of Tuple from a specific table with Entity Framework / LINQ?
- In C#, how can i determine if a list has any item from another list?
- Can I directly "Extract" an Item from a List<>?
- How can I extract data from a list of parent objects with children?
- How can I get the index of an item in a list in a single step?
- How can I get every nth item from a List<T>?
- Remove item from list based on condition
- Check if list contains item from other list in EntityFramework
- Select single item from a list
- Excluding one item from list (by Index), and take all others
- C# Extract list of fields from list of class
- Remove item from list using linq
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Generating the Shortest Regex Dynamically from a source List of Strings
- Remove a specific item from a list using LINQ
- LINQ select List where sub-list contains item from another list
- Can I populate a list of my own class directly in LINQ?
- Is there a better way to return the next item in a list and loop from the end to the front?
- Using LINQ to extract ints from a list of strings
- Get first item in list from linq query
- Remove Duplicate item from list based on condition
- LINQ: Can we create a flat list from hierarchy
- Can I use LINQ to create a new list of objects from an existing list
- can my code improve from using LINQ?
- How can I cast a List into a type which inherits from List<T>?
- How can I take only entries from even positions in List
- How can I retrieve a set of unique arrays from a list of arrays using LINQ?
- Removing a single item from an enumerable source when the items are equal
- Remove All from List where each line doesn't contain any item from another list
More Query from same tag
- How to group data from table with multiple columns
- linq after groupby unable to get column values
- linq where -> compare to list of longs
- C#: Remove duplicate values from dictionary?
- Select values from two tables using “WHERE NOT IN” and DISTINCT in LINQ in Linq to Sql
- A range of objects from a list
- The specified type member 'TimeBandDescription' is not supported in LINQ to Entities
- c# Remove rows from csv
- Adding string to IEnumerable of type string using LINQ to entities
- JArray Strings to JArray of Objects
- Conditional Output by Linq to XML
- LINQ filter LIST values
- how to delete multiple rows of data with linq to EF using DbContext
- Find XmlElement in List<> using array of matching attribute names ... Using Linq
- Merge multiple word documents into one using OpenXML and XElement
- Operators "AND" "OR" SQL
- Join rows using C# Linq
- Linq date and datetime comparer
- SQL Join table and SUM values
- Updating every item in a table with Linq / C#
- Linq Stored Uppercase table columns
- c# Datatable force DataType when using Adapter fill
- entity framework - inner join to left join
- Overload for LINQ List.Contains() to accept column name
- Getting Only The Top Row From Each Group
- Linq & SQL: Counting the number of people living in a place per year with only the start and end dates of their residencies
- C# Linq: Can you merge DataContexts?
- How do i use an Expression<Func<T>> in a linq statement if my navigation property is a single item?
- How can i speed up count of big Linq query?
- Linq orderby calculation