Essential Things in Generic List C# that every developer must know

In this article we will go with all important operations of generic list.

1. Initializing Lists

You can initialize a list with predefined values using collection initializer syntax.

//Old way List<Employee> list = new List<Employee>(); list.Add(new Employee(1, "Devesh omar", "Developer")); list.Add(new Employee(2, "Ram kumar", "Manager")); list.Add(new Employee(4, "Pintu Sachin", "Intern")) list.Add(new Employee(5, "Sachin Raj", "Analyst")) // new way var employees = new List<Employee> { new Employee(1, "Devesh omar", "Developer"), new Employee(2, "Ram kumar", "Manager"), new Employee(3, "S Kumar", "Designer"), new Employee(4, "Pintu Sachin", "Intern"), new Employee(5, "Sachin Raj", "Analyst") };



2. Finding Items

You can use LINQ methods to find items in a list. we are going to search based on id =1

var employee = employees.FirstOrDefault(e => e.Id == 1); if (employee != null) { Console.WriteLine($"Found: {employee.Name}"); }



3. Checking for Existence

Use Any to check if any item in the list matches a condition.

bool exists = employees.Any(e => e.Id == 1); Console.WriteLine($"Employee exists: {exists}");


4. Removing Items by Condition

You can remove items that match a certain condition using RemoveAll.

employees.RemoveAll(e => e.Position == "Manager");


We can see Manager record deleted.

5. Sorting Lists

Use the Sort method to sort a list based on a property. We have sorted based on name

employees.Sort((x, y) => x.Name.CompareTo(y.Name));





6. Efficiently Cloning a List

clone a list using the ToList method.

var clonedList = employees.ToList();

7. Using LINQ for Complex Queries
we are have written linq to search data and order data.

var developers = employees.Where(e => e.Position == "Developer") .OrderBy(e => e.Name) .ToList(); developers.ForEach(e => Console.WriteLine(e.Name));

 


8. Grouping Items

Group items based on a property using GroupBy.

var groupedByPosition = employees.GroupBy(e => e.Position); foreach (var group in groupedByPosition) { Console.WriteLine($"Position: {group.Key}"); foreach (var employee in group) { Console.WriteLine($" {employee.Name}"); } }

9. Converting a List to Another Collection Type

Convert a list to an array or other collection types.

Employee[] employeeArray = employees.ToArray(); Dictionary<int, Employee> employeeDictionary = employees.ToDictionary(e => e.Id);


10.Using ForEach

You can use the ForEach method to perform an action on each item in the list.

employees.ForEach(e => Console.WriteLine(e.Name));


11 .Adding a Range of Items

Add multiple items to a list at once using AddRange.


var employees = new List<Employee> { new Employee(1, "Devesh omar", "Developer"), new Employee(2, "Ram kumar", "Manager"), new Employee(3, "S Kumar", "Designer") }; // adding var newEmployees = new List<Employee> { new Employee(4, "Pintu Sachin", "Intern"), new Employee(5, "Sachin Raj", "Analyst") }; employees.AddRange(newEmployees);



In this article we have learnt all major function and operation of generic list.


line

Copyrights © 2024 letsupdateskills All rights reserved