Mega Code Archive

 
Categories / C# Tutorial / Class
 

Using a string as an indexer value

class DayCollection {     string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };     private int GetDay(string testDay)     {         for(int j = 0; j < days.Length; j++)         {                    if (days[j] == testDay)             {                 return j;             }         }         throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form \"Sun\", \"Mon\", etc");     }     public int this[string day]     {         get         {             return (GetDay(day));         }     } } class Program {     static void Main(string[] args)     {         DayCollection week = new DayCollection();         System.Console.WriteLine(week["Fri"]);         System.Console.WriteLine(week["AAA"]);     } }