CRUD Operations in Generic List C#

In this tutorial we will learn very basic CRUD operations in C#

Here CRUD means

C-Create

R-Read

U-Update

D-Delete

Consider EMP class as below


csharp
public class Employee { public int Id { get; set; } public string Name { get; set; } public string Position { get; set; } public Employee(int id, string name, string position) { Id = id; Name = name; Position = position; } }


Consider EmployeeRepository Class

We have defined Add, Update, Delete Functions this class


csharp
public class EmployeeRepository { private List<Employee> _employees; public EmployeeRepository() { _employees = new List<Employee>(); } // Create operation public void AddEmployee(Employee employee) { _employees.Add(employee); } // Read operation public List<Employee> GetAllEmployees() { return _employees; } public Employee GetEmployeeById(int id) { return _employees.FirstOrDefault(e => e.Id == id); } // Update operation public bool UpdateEmployee(int id, string newName, string newPosition) { var employee = GetEmployeeById(id); if (employee != null) { employee.Name = newName; employee.Position = newPosition; return true; } return false; } // Delete operation public bool DeleteEmployee(int id) { var employee = GetEmployeeById(id); if (employee != null) { _employees.Remove(employee); return true; } return false; } } }

Explanation

Employee Class: A simple class with properties and a constructor for Id, Name, and Position.

  1. EmployeeRepository Class: Encapsulates the list of employees and provides methods for CRUD operations.
  2. AddEmployee: Adds a new employee to the list.
  3. GetAllEmployees: Returns all employees.
  4. GetEmployeeById: Returns an employee by ID.
  5. UpdateEmployee: Updates an employee's name and position by ID.
  6. DeleteEmployee: Deletes an employee by ID.


Add Employees to List and Display

csharp
using CRUD_Operations; Console.WriteLine("Welcome to ....Lets update Skills!!"); var repository = new EmployeeRepository(); // Add employees repository.AddEmployee(new Employee(1, "Devesh omar", "Developer")); repository.AddEmployee(new Employee(2, "Hillary ", "Architech")); repository.AddEmployee(new Employee(3, "Vinu ", "Manager")); repository.AddEmployee(new Employee(4, "Pankaj ", "Sr. Manager")); // Read employees Console.WriteLine("Employees after creation:"); foreach (var employee in repository.GetAllEmployees()) { Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); }


Output



Update : we have updated the emp based on id, in our code we have updated emp where id=2

using CRUD_Operations;

csharp
Console.WriteLine("Welcome to ....Lets update Skills!!"); var repository = new EmployeeRepository(); // Add employees repository.AddEmployee(new Employee(1, "Devesh omar", "Developer")); repository.AddEmployee(new Employee(2, "Hillary ", "Architech")); repository.AddEmployee(new Employee(3, "Vinu ", "Manager")); repository.AddEmployee(new Employee(4, "Pankaj ", "Sr. Manager")); // Read employees Console.WriteLine("Employees after creation:"); foreach (var employee in repository.GetAllEmployees()) { Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); } // Update an employee repository.UpdateEmployee(2, "Kumar Devesh ", "Senior Product "); Console.WriteLine("\nEmployees after update:"); foreach (var employee in repository.GetAllEmployees()) { Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); }


Output



Delete


csharp
using CRUD_Operations; Console.WriteLine("Welcome to ....Lets update Skills!!"); var repository = new EmployeeRepository(); // Add employees repository.AddEmployee(new Employee(1, "Devesh omar", "Developer")); repository.AddEmployee(new Employee(2, "Hillary ", "Architech")); repository.AddEmployee(new Employee(3, "Vinu ", "Manager")); repository.AddEmployee(new Employee(4, "Pankaj ", "Sr. Manager")); // Read employees Console.WriteLine("Employees after creation:"); foreach (var employee in repository.GetAllEmployees()) { Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); } // Update an employee repository.UpdateEmployee(2, "Kumar Devesh ", "Senior Product "); Console.WriteLine("\nEmployees after update:"); foreach (var employee in repository.GetAllEmployees()) { Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); } // Delete an employee repository.DeleteEmployee(1); Console.WriteLine("\nEmployees after deletion:"); foreach (var employee in repository.GetAllEmployees()) { Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); } Console.ReadKey();


We have added a code to delete based on Id

we have deleted where id=1, in output we will have 3 records now.


Thanks.

We have learnt very basic of CRUD operation in Generic List C#

line

Copyrights © 2024 letsupdateskills All rights reserved