Advertisement
elena1234

How to add indexers in c#

Mar 22nd, 2021 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1.     public class Employee
  2.     {
  3.         public Employee(string name, int id)
  4.         {
  5.             this.Name = name;
  6.             int ID = id;
  7.  
  8.         }
  9.         public string Name { get; set; }
  10.         public  int ID { get; set; }
  11.     }
  12.     public class Company
  13.     {
  14.         private List<Employee> listWithEmployees;
  15.         public Company()
  16.         {
  17.             this.listWithEmployees = new List<Employee>();
  18.         }
  19.  
  20.         public void AddEmployee(Employee employee)
  21.         {
  22.             this.listWithEmployees.Add(employee);
  23.         }
  24.  
  25.         public string this[int employeeID]
  26.         {
  27.             get
  28.             {
  29.                 return listWithEmployees.FirstOrDefault(e => e.ID == employeeID).Name;
  30.             }
  31.             set
  32.             {
  33.                 listWithEmployees.FirstOrDefault(e => e.ID == employeeID).Name = value;
  34.             }
  35.         }        
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement