C#索引器2 字符串作为索引号

    6.索引器   字符串作为索引号    

public class Demo    

{        

        private Hashtable name = new Hashtable();          

        public string this[string index]        

        {            

               set            

              {                

                    name.Add(index, value);            

              }            

              get            

             {                

                     return name[index].ToString();            

             }       

       }

    }

   

public class Test    

{        

        public static void Main(string[] args)        

        {            

               Demo dd = new Demo();            

               dd["A"] = "SB";            

               dd["B"] = "EB";

               Console.WriteLine(dd["A"]);            

               Console.WriteLine(dd["B"]);          

             //Console.WriteLine(dd["C"]);           

               Console.ReadKey();        

        }    

}

posted on 2016-03-08 11:31  可爱的春哥  阅读(1213)  评论(0编辑  收藏  举报

导航