zoukankan      html  css  js  c++  java
  • 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();        

             }    

    }

  • 相关阅读:
    typeof与instanceof
    TCP与UDP的区别
    const、let、var关键字
    基本通用的使用jdbc文件java代码连接数据库
    HTML知识点01
    ADO.NET基础02
    ADO.NET基础03
    数据库语法01
    数据库语法02
    Ubuntu16.4 内核降级
  • 原文地址:https://www.cnblogs.com/gyc19920704/p/5254972.html
Copyright © 2011-2022 走看看