首页 > Windows开发 > 详细

C#索引器3

时间:2016-03-08 19:32:24      阅读:239      评论:0      收藏:0      [点我收藏+]

    7.索引器  重载    

public class Demo    

{        

          private Hashtable name = new Hashtable();          

          public string this[int index]        

         {            

                 get            

                 {                

                      return name[index].ToString();            

                 }            

                 set            

                 {                

                       name.Add(index, value);         

                 }        

          }

           public int this[string index]        

           {            

                  set            

                 {                

                        name.Add(value, index);            

                 }            

                 get            

                {                

                        foreach (DictionaryEntry dd in name)                

                        {                    

                               if (dd.Value.ToString() == index)                    

                              {                        

                                     return Convert.ToInt32(dd.Key);                    

                              }                

                       }                

                       return -1;            

                }        

          }    

}

 

public class Test    

{        

        public static void Main(string[] args)        

        {            

               Demo dd = new Demo();            

               dd[3] = "Lobin";            

               dd[4] = "Jack";            

               Console.WriteLine("3号 是 {0}",dd[3]);            

               Console.WriteLine("4号 是 {0}",dd[4]);            

               Console.WriteLine("Lobin的编号为: {0}", dd["Lobin"]);            

               Console.WriteLine("Jack的编号为: {0}",dd["Jack"]);            

               Console.ReadKey();

               dd["Mark"] = 5;            

               Console.WriteLine("Mark的编号为 {0}", dd["Mark"]);            

               Console.ReadKey();        

         }    

}

 

C#索引器3

原文:http://www.cnblogs.com/gyc19920704/p/5254972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!