C# indexers are like properties. It allows to access instances like an array with an index.
It always represents collection objects but exposes arguments using which we can use to identify a particular element in the collection at a time.
We can access an indexer by indexing the class instance with the appropriate index parameter.
We can overload indexers with different data types and create multiple indexers for a class to expose & access different collections using the same class instance.
It acts as a member of a class but is used to access a data item of a collection from outside the class.
When do we use it?
Let's say we have to load a collection object in memory and we don't know upfront exactly how much memory it would take. Indexers help to set up an approach to load a small chunk of the entire data load, process it, and then move on to the next chunk.
It helps to abstract collection type in the model from the usage class as it always allows to access one data item at a time. This helps code easier to understand and maintain.
Indexer should be used if providing an index to an object makes sense.
Simple Example:
publicclassEmployee{publicstringName{get;set;}publicintId{get;set;}publicstringDepartment{get;set;}publicintDepartmentId{get;set;}publicstringCity{get;set;}publicstringZip{get;set;}}publicenumDepartmentEnum{None=0,Engineering,Accounting,Economics,Politics}internalclassProgram{privatestaticvoidMain(string[]args){Console.WriteLine("Hello, World!");varprocessor=newEmployeeProcessor(DepartmentEnum.Engineering);processor.ProcessPayroll();processor.ProcessHealthInsurance();Console.WriteLine($"Total engineering employees - {processor[DepartmentEnum.Engineering].Count()}");Console.ReadKey();}}publicclassEmployeeProcessor{privateList<Employee>employees=newList<Employee>();publicEmployeeProcessor(DepartmentEnumdepartment){LoadEmployeesByDepartment(department);}publicList<Employee>this[DepartmentEnumdepartment]{get{returnemployees.Where(x=>x.DepartmentId==(int)department).ToList();}}privatevoidLoadEmployeesByDepartment(DepartmentEnumdepartment){//load it from the data source. I am Hard coding for a blog if(department==DepartmentEnum.Engineering){employees.Add(newEmployee(){Name="Nirmal kumar",Id=1,Department="Engineering",DepartmentId=1,City="Raleigh",Zip="27616"});}elseif(department==DepartmentEnum.Accounting){employees.Add(newEmployee(){Name="Preethi",Id=1,Department="Accounting",DepartmentId=2,City="Atlanta",Zip="12345"});}elseif(department==DepartmentEnum.Economics){employees.Add(newEmployee(){Name="Adithi",Id=1,Department="Economics",DepartmentId=3,City="New york",Zip="23456"});employees.Add(newEmployee(){Name="Pranavi",Id=2,Department="Economics",DepartmentId=3,City="New york",Zip="23456"});}}publicvoidProcessPayroll(){}publicvoidProcessHealthInsurance(){}}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)