zoukankan      html  css  js  c++  java
  • C#中使用Foreach

    用foreach遍历访问的对象需要实现 IEnumerable  接口或声明 GetEnumerator 方法的类型。

    先来看看IEnumerable接口:
    namespace System.Collections
    {
        // Summary:
        //     Exposes the enumerator, which supports a simple iteration over a non-generic
        //     collection.
        [ComVisible(true)]
        [Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
        public interface IEnumerable
        {
            // Summary:
            //     Returns an enumerator that iterates through a collection.
            //
            // Returns:
            //     An System.Collections.IEnumerator object that can be used to iterate through
            //     the collection.
            [DispId(-4)]
            IEnumerator GetEnumerator();
        }
    }

    IEnumerator接口:

    namespace System.Collections
    {
        // Summary:
        //     Supports a simple iteration over a nongeneric collection.
         [ComVisible(true)]
         [Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
         public interface IEnumerator
        {
    
             object Current { get; }
     
             bool MoveNext();
    
             void Reset();
         }
     }

    示例:

    namespace ForeachDemo
    {
        public class MySplit : IEnumerable
        {
            private string[] elements;
    
            public MySplit(string source,char[] delimiters)
            {
                elements = source.Split(delimiters);
            }
    
            public string[] Elements
            {
                get
                {
                    return elements;
                }
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return new MyEnumerator(this);
            }
        }
    
        public class MyEnumerator : IEnumerator
        {
            private int position = -1;
            private MySplit t;
    
            public MyEnumerator(MySplit t)
            {
                this.t = t;
            }
    
            public bool MoveNext()
            {
               if (position<t.Elements.Length - 1)
               {
                   position++;
                   return true;
               }
               else
               {
                   return false;
               }
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            object IEnumerator.Current
            {
                get
                {
                    try
                    {
                        return t.Elements[position];
                    }
                    catch(IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                MySplit mysplit = new MySplit("i am a good student.",new char[]{' '});
    
                foreach(string item in mysplit)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }

    输出:

     
  • 相关阅读:
    AI.框架理论.语义网.语言间距.孤单
    图像局部显著性—点特征(Fast)
    图像的全局特征--HOG特征、DPM特征
    图像局部显著性—点特征(FREAK)
    iis7服务器隐藏index.php
    php命名空间
    thinkphp5.0 composer安装phpmailer
    php输入流简单小例子
    js、php判断手机PC
    thinkphp5.0 空模块、空控制器、空方法
  • 原文地址:https://www.cnblogs.com/zzunstu/p/3393930.html
Copyright © 2011-2022 走看看