List Class important methods in C#

Generic List Class important methods in C#

In this article, we will discuss some Generic List Class important methods.

TrueForAll():

This method returns true or false depending on whether if every element in the list matches the conditions defined by the specified predicate.

AsReadOnly():

This method returns a read-only wrapper for the current collection. Use this method, if you don’t want the client to modify the collection i.e. add or remove any elements from the collection. The ReadOnlyCollection will not have methods to add or remove items from the collection. We can only read items from this collection.

TrimExcess():

This method sets the capacity to the actual number of elements in the List if that number is less than a threshold value. 

According to MSDN: 

This method can be used to minimize a collection’s memory overhead if no new elements will be added to the collection. The cost of reallocating and copying a large List<T> can be considerable, however, so the TrimExcess method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain. The current threshold is 90 percent, but this could change in the future.

Let us understand the above methods with an example.
namespace ListCollectionMethodDemo
{
    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 = 4500
            };

            Employee emp4 = new Employee()
            {
                ID = 104,
                Name = "Sambit",
                Gender = "Male",
                Salary = 6500
            };
            
            List<Employee> listEmployees = new List<Employee>(100);
            listEmployees.Add(emp1);
            listEmployees.Add(emp2);
            listEmployees.Add(emp3);
            listEmployees.Add(emp4);
            
            //TrueForAll
            Console.WriteLine("Are all salaries greater than 5000: "
                            + listEmployees.TrueForAll(x => x.Salary > 5000));

            // ReadOnlyCollection will not have Add() or Remove() methods
            System.Collections.ObjectModel.ReadOnlyCollection<Employee>
                            readOnlyEmployees = listEmployees.AsReadOnly();
            Console.WriteLine("Total Items in ReadOnlyCollection = " +
                            readOnlyEmployees.Count);

            // listEmployees list is created with an initial capacity of 50
            // but only 4 items are in the list. The filled percentage is 
            // less than 90 percent threshold.
            Console.WriteLine("List capacity before invoking TrimExcess = " +
                            listEmployees.Capacity);
            // Invoke TrimExcess() to set the capacity to the actual 
            // number of elements in the List
            listEmployees.TrimExcess();
            Console.WriteLine("List capacity After invoking TrimExcess = " + 
                            listEmployees.Capacity);
            
            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 important methods in C#

SUMMARY:

In this article, I try to explain Generic List Class important 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 *