• C#索引器3 重载


        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();        

             }    

    }

  • 相关阅读:
    如何在Web项目中给没有添加API核心组件添加APIController的帮助页HelpPage
    如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试
    exception throw in progress runner thread_VS2015中SVN源代码无说明提交异常
    [转]AMBA、AHB、APB、ASB总线简介
    UML和模式应用4:初始阶段(6)--迭代方法中如何使用用例
    UML和模式应用4:初始阶段(5)--用例编写的准则
    startup_MK64F12.s文件解析
    [转] bss段、data段、text段
    [转]GDB-----2.watchpoint
    [转]GDB-----1.GDB概述
  • 原文地址:https://www.cnblogs.com/gyc19920704/p/5254972.html
Copyright © 2020-2023  润新知