请问C#中可否定义多个索引器 如果可以 那么要怎么样去访问才不会冲突

解决方案 »

  1.   

    多个像this一样的索引吗?不太好实现吧,要不就索引里返回索引吧。
      

  2.   

    可以定义多个索引,类型不同就行
     public class A
            {            
                public string this[int index]
                {
                    get { return index.ToString(); }
                }
                public string this[string name]
                {                get { return name; }
                }
            }
      

  3.   

    根据参数个数
    例如:
        public int this[int index]
        {
          get
            { // code here}
          set
            { // code here}
         }
        public int this[int index,int index]
        {
          get
            { // code here}
          set
            { // code here}
         }
         public int this[string index]
        {
          get
            { // code here}
          set
            { // code here}
         }
         public int this[string index,string index]
        {
          get
            { // code here}
          set
            { // code here}
         }
     等等,这就是索引器重载 与返回类型  没关系  只与 参数个数和参数类型有关  再次索引器没有名称