Define both int and string indexer for a class : Indexer : Class C# Examples


C# Examples » Class » Indexer »

 

Define both int and string indexer for a class









    
using  System;
using  System.Collections;
using  System.Collections.Specialized;

public  class  MainClass
{
    public  static  void  Main()
    {
        EmployeeList  carLot  =  new  EmployeeList();
        
        carLot["A"]  =  new  Employee("A");
        carLot["B"]  =  new  Employee("B");
        carLot["C"]  =  new  Employee("C");

        Employee  zippy  =  carLot["C"];
        Console.WriteLine(zippy.Name);
    }
}

public  class  EmployeeList
{
    private  ListDictionary  carDictionary;
    
    public  EmployeeList()
    {
        carDictionary  =  new  ListDictionary();
    }

    //  The  string  indexer.
    public  Employee  this[string  name]
    {
        get  {  return  (Employee)carDictionary[name];}
        set  {  carDictionary.Add(name,  value);}
    }
    
    //  The  int  indexer.
    public  Employee  this[int  item]
    {
        get  {  return  (Employee)carDictionary[item];}
        set  {  carDictionary.Add(item,  value);}
    }
}
public  class  Employee
{
        public  string  Name  =  "";
        
    public  Employee(string  n)
    {
            Name  =  n;
    }
}
    
   
  
   



Output

C


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Indexer