zoukankan      html  css  js  c++  java
  • 自定义类索引(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

  • 相关阅读:
    [Real World Haskell翻译]第24章 并发和多核编程 第一部分并发编程
    [Real World Haskell翻译]第22章 扩展示例:Web客户端编程
    [Real World Haskell翻译]第27章 网络通信和系统日志 Sockets and Syslog
    druid 解密
    git clone 所有远程分支到本地
    十、Delphi 把Json转成Delphi实体类
    XML External Entity attack/XXE攻击
    大家好!
    XXE攻防——XML外部实体注入
    ubuntu安装Go环境
  • 原文地址:https://www.cnblogs.com/jasenkin/p/1680775.html
Copyright © 2011-2022 走看看