Generic List Class Range Methods in C#

Generic List Class Range Methods in C#

In this article, I am going to discuss the Generic List class Range Methods in C# with Examples. Please read our previous article before proceeding to this article where we discussed the basics of Generic List class with examples. The Generic List collection class provides some useful range methods. You can use those range methods to insert or remove a range of elements or items from a list.

The Generic List class provides the following Range methods.
AddRange() Method: 

As we already discussed the Add() method of the List class allows us to add only one item at the end of the collection. If you want to add another list of items to the list collection then you need to the AddRange() method.
Syntax:
AddRange(IEnumerable<T>)

GetRange() Method: 

In our previous article, we discussed that by using the index, we can retrieve only one element from the collection. In many real-time scenarios, we may need to retrieve a list of items from a collection. Then in such scenarios, we need to use the GetRange() method of the List class. The GetRange() method takes 2 parameters. The first parameter is the starting index position and the second parameter is the number of items to return from the list.
Syntax:
GetRange(Int32, Int32)

InsertRange() Method:

The Insert() method of the Generic List collection class allows us to insert an element at a specified position by using the index which it takes as the input parameter. If you want to insert another list of elements at a specified index, then you need to use the InsertRange() method of the List class. This method takes two parameters. The first parameter is the index position where it will insert the elements and the second parameter is the list of items that you want to insert into the collection.
Syntax:
InsertRange(Int32, IEnumerable<T>)

RemoveRange() Method:

If you want to remove a range of elements from a specified index position then you need to use the RemoveRange() method. This RemoveRange() method takes 2 parameters. The first parameter is the start index in the list and the second parameter is the number of elements to remove from the list.
Syntax:
RemoveRange(Int32, Int32)

Note: The Remove method is used to remove only the first matching element from the list and the RemoveAt() method removes an element from a specified index in the list. 

Clear() Method:

The Clear() method of the List class is used to remove all the items from the list collection.
Syntax:
Clear()

Let us see an example to understand the Generic List class Range Methods in C#.

using System;
using System.Collections.Generic;

namespace ListCollectionrangeMethodDemo
{
    public class Program
    {
        public static void Main()
        {
            Employee emp1 = new Employee()
            {
                ID = 101,
                Name = "Pranaya",
                Gender = "Male",
                Salary = 5000
            };
            Employee emp2 = new Employee()
            {
                ID = 102,
                Name = "Priyanka",
                Gender = "Female",
                Salary = 7000
            };
            Employee emp3 = new Employee()
            {
                ID = 103,
                Name = "Anurag",
                Gender = "Male",
                Salary = 5500
            };
            Employee emp4 = new Employee()
            {
                ID = 104,
                Name = "Sambit",
                Gender = "Male",
                Salary = 6500
            };
            Employee emp5 = new Employee()
            {
                ID = 105,
                Name = "Hina",
                Gender = "Female",
                Salary = 7500
            };
            Employee emp6 = new Employee()
            {
                ID = 106,
                Name = "Tarun",
                Gender = "Male",
                Salary = 8500
            };

            List<Employee> listEmployees = new List<Employee>();
            Console.WriteLine("Add and AddRange Method");

            //Use Add() method to add one item at a time to the end of the list
            listEmployees.Add(emp1);
            listEmployees.Add(emp2);
            listEmployees.Add(emp3);

            //Create another list
            List<Employee> AnotherlistEmployees = new List<Employee>();
            AnotherlistEmployees.Add(emp4);
            AnotherlistEmployees.Add(emp5);
            AnotherlistEmployees.Add(emp6);
            
            //Use AddRange() method to add another list of items
            listEmployees.AddRange(AnotherlistEmployees);
            foreach (Employee employee in listEmployees)
            {
                Console.WriteLine("ID = {0}, Name = {1},  Gender = {2}, Salary = {3}",
                    employee.ID, employee.Name, employee.Gender, employee.Salary);
            }
            Console.WriteLine();

            Console.WriteLine("GetRange Method");

            //Use GetRange() method to returns a range of items from the list.
            List<Employee> ListOfNewEmployees = listEmployees.GetRange(3, 3);
            foreach (Employee employee in ListOfNewEmployees)
            {
                Console.WriteLine("ID = {0}, Name = {1},  Gender = {2}, Salary = {3}",
                    employee.ID, employee.Name, employee.Gender, employee.Salary);
            }
            Console.WriteLine();

            Console.WriteLine("Remove RemoveAt and RemoveAll");

            //Use Remove() method to removes only the first matching item from the list.
            listEmployees.Remove(emp1);

            //Use RemoveAt() method to remove an item from a specified index.
            // listEmployees.RemoveAt(0);

            //Use RemoveAll() methid to removes all the items from a collection
            // that matches the specified condition.
            listEmployees.RemoveAll(x => x.Gender == "Female");
            foreach (Employee employee in listEmployees)
            {
                Console.WriteLine("ID = {0}, Name = {1},  Gender = {2}, Salary = {3}",
                    employee.ID, employee.Name, employee.Gender, employee.Salary);
            }
            Console.WriteLine();

            Console.WriteLine("RemoveRange Method");

            // Use RemoveRange() method to removes a range of elements from the list. 
            listEmployees.RemoveRange(0, 2);
            foreach (Employee employee in listEmployees)
            {
                Console.WriteLine("ID = {0}, Name = {1},  Gender = {2}, Salary = {3}",
                    employee.ID, employee.Name, employee.Gender, employee.Salary);
            }
            Console.WriteLine();

            Console.WriteLine("Insert and InsertRange Method");

            //Use Insert() method to insert a single item at a specfic position 
            listEmployees.Insert(0, emp1);
            listEmployees.Insert(1, emp2);

            //Use InsertRange() method to insert another list of items at a specified position
            listEmployees.InsertRange(0, AnotherlistEmployees);
            foreach (Employee employee in listEmployees)
            {
                Console.WriteLine("ID = {0}, Name = {1},  Gender = {2}, Salary = {3}",
                    employee.ID, employee.Name, employee.Gender, employee.Salary);
            }
            Console.WriteLine();

            Console.WriteLine("Clear Method");

            // Use Clear method to remove all the items from the list collection
            listEmployees.Clear();
            Console.WriteLine("Total Items in the List After Clear function = " + listEmployees.Count);
            Console.ReadKey();
        }
    }
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Salary { get; set; }
    }
}

OUTPUT:

Generic List Class Range Methods in C#

In the next article, I am going to discuss how to sort a list of simple type in C# with examples.

SUMMARY:

In this article, I try to explain the Generic List Class Range Methods in C# with an example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *