TechieClues TechieClues
Updated date Jan 16, 2023
In this blog, we will see how to remove the last element from a list in C#

Remove the Last Element From a List in C#

  • Using RemoveAt() Method
  • Using List<T>.RemoveRange() Method
  • Using Enumerable.Take() Method
  • Using SkipLast() from Linq (.NET Core)

Remove the last element from a list using RemoveAt() Method

The RemoveAt() method is used to remove the specified element in a list. In order to remove the last element, we will have to pass the last index to the RemoveAt() method as shown below,

using System;
using System.Collections.Generic;

namespace RemoveLastElementFromList
{
    class Program
    {
        static void Main()
        {
            // list of items
            List<string> cars = new List<string>() { "Madza", "Tesla", "Skoda", "Suzuki", "BMW" };

            // Remove the last element from the list
            cars.RemoveAt(cars.Count - 1);

            // Display the element
            foreach (string car in cars)
                Console.WriteLine(car);
            
            Console.ReadKey();
        }
    }
}

Output:

Madza
Tesla
Skoda
Suzuki

Remove the last element from a list using List<T>.RemoveRange() Method

The List<T>.RemoveRange() method removes a range of elements from a list. The RemoveRange() method accepts the starting index of elements and the number of elements to remove. The below example shows to remove the last element from a list.

using System;
using System.Collections.Generic;

namespace RemoveLastElementFromList
{
    class Program
    {
        static void Main()
        {
            // list of items
            List<string> cars = new List<string>() { "Madza", "Tesla", "Skoda", "Suzuki", "BMW" };

            // Remove the last element from the list
            if (cars.Count > 0)
            {
                cars.RemoveRange(cars.Count - 1, 1);
            }

            // Display the element
            foreach (string car in cars)
                Console.WriteLine(car);
            
            Console.ReadKey();
        }
    }
}

 Output:

Madza
Tesla
Skoda
Suzuki

Remove the last element from a list using Enumerable.Take() Method

The Take() method returns the specified number of elements from the start of the input sequence. In the below example, we remove the last index using cars.Count() - 1 and the Take() method creates the new list except the last element as shown below,

using System;
using System.Linq;
using System.Collections.Generic;

namespace RemoveLastElementFromList
{
    class Program
    {
        static void Main()
        {
            // list of items
            List<string> cars = new List<string>() { "Toyota", "Tesla", "Skoda", "Audi", "BMW" };

            // Remove the last element from the list
            var result = cars.Take(cars.Count() - 1).ToList();

            // Display the element
            foreach (string item in result)
                Console.WriteLine(item);
            
            Console.ReadKey();
        }
    }
}

  Output:

Toyota
Tesla
Skoda
Audi

Remove the last element from a list using SkipLast() from Linq (.NET Core)

The SkipLast() method removes the specified elements from the end and returns the remaining elements. In the following example, SkipLast() removes the last element from the list as shows below,

using System;
using System.Linq;
using System.Collections.Generic;

namespace RemoveLastElementFromList
{
    class Program
    {
        static void Main()
        {
            // list of items
            List<string> cars = new List<string>() { "Madza", "Tesla", "Skoda", "Suzuki", "BMW" };

            // Remove the last element from the list
            List<int> result = cars.SkipLast(1).ToList();

            // Display the element
            foreach (string item in result)
                Console.WriteLine(item);
            
            Console.ReadKey();
        }
    }
}

   Output:

Toyota
Tesla
Skoda
Audi

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!