自定义类索引(int,string)与foreach简单随笔

      索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。它可以使得像数组那样对对象使用下标,当然也可以使用其他类型进行索引,比如字符串等。它提供了通过索引方式方便地访问类的数据信息的方法。
  要声明类或结构上的索引器,请使用this关键字,例如:
  public int this[int index] //声明索引器
  {
  // get and set 访问

  }

     foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。

  1  using  System;
  2  using  System.Collections.Generic;
  3  using  System.Linq;
  4  using  System.Text;
  5  using  System.Collections;
  6 
  7  /*
  8   如何才能让这个类支持foreach呢?其实很简单,只要让我们的类继承自IEnumerable接口,并对该接口中的GetEnumerator()方法加以实现就可以了.
  9  */
 10 
 11  namespace  IedexSample
 12  {
 13       class  Program
 14      {
 15           static   void  Main( string [] args)
 16          {
 17             /*   int NUM = 50;
 18            //  IndexSample<string> indexStrs = new IndexSample<string>();
 19              IndexSample<string> indexStrs = new IndexSample<string>(NUM);
 20              try
 21              {
 22                  for (int i = 0; i < 10; i++)
 23                  {
 24                      indexStrs[i] = "Sample" + i.ToString();
 25                  }
 26                  foreach (string str in indexStrs)
 27                  {
 28                      Console.WriteLine(str);
 29                  }
 30              }
 31              catch (IndexOutOfRangeException ex)
 32              {
 33                  Console.WriteLine(ex.ToString());
 34              } */  //上面的为IndexSample的索引用INT
                    //下面的用string来索引
 35 
 36              IndexerSample < string >  indexStrs  =   new  IndexerSample < string > (); // T  string 
 37               for ( int  i = 0 ;i < 10 ;i ++ )
 38              {
 39                  indexStrs[i.ToString()]  =   " sample "   +  i.ToString();
 40              }
 41               foreach ( string   str  in  indexStrs)
 42              { // 遍历所有value;
 43                  Console.WriteLine(str);
 44              }
 45              Console.ReadLine();
 46          }
 47      }
 48       public   class  IndexSample < T >  : IEnumerable
 49      {
 50          T[] array  =   null ;
 51           public  IndexSample( int  number)
 52          {
 53              array  =   new  T[number];
 54          }
 55           public  IndexSample( ) 
 56          {
 57             array  =   new  T[ 100 ];
 58          }
 59           public  T  this [ int  i]
 60          {
 61               get
 62              {
 63                   return  array[i];
 64              }
 65               set
 66              {
 67                  array[i]  =  value;
 68              }
 69          }
 70           public  IEnumerator GetEnumerator()
 71          {
 72               for  ( int  i  =   0 ; i  <  array.Length; i ++ )
 73              {
 74                   yield   return   this [i];
 75              }
 76          }
 77      }
 78 
 79       public   class  IndexerSample < T >  : IEnumerable < T >
 80      {
 81          Dictionary < string  ,T >  dic = null ;
 82           public  IndexerSample() 
 83          {
 84               this .dic  =   new  Dictionary < string , T > ();
 85          }
 86           public  T  this [ string  indexStr]
 87          {
 88               get  
 89              {
 90                   if  (dic.ContainsKey(indexStr))
 91                  {
 92                       return  dic[indexStr];
 93                  }
 94                   else  
 95                  { 
 96                       return   default (T); 
 97                  }
 98              }
 99               set  
100              {
101                  dic[indexStr]  =  value;
102              }
103          }
104           public   int  Count
105          {
106               get
107              {
108                   return  dic.Count;
109              }
110          }
111          IEnumerator IEnumerable.GetEnumerator() 
112          {
113               return  dic.Values.GetEnumerator();
114          }
115          IEnumerator < T >  System.Collections.Generic.IEnumerable < T > .GetEnumerator() 
116          {
117               return  dic.Values.GetEnumerator();
118          }
119      }
120  }
121 

 

源代码:/Files/jasenkin/IedexSample.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值