Csharp/CSharp Tutorial/Data Structure/IEnumerable

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Counting Enumerable

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.ruponentModel;

       class CountingEnumerable : IEnumerable<int>
       {
           public IEnumerator<int> GetEnumerator()
           {
               return new CountingEnumerator();
           }
           IEnumerator IEnumerable.GetEnumerator()
           {
               return GetEnumerator();
           }
       }
       class CountingEnumerator : IEnumerator<int>
       {
           int current = -1;
           public bool MoveNext()
           {
               current++;
               return current < 10;
           }
           public int Current
           {
               get { return current; }
           }
           object IEnumerator.Current
           {
               get { return Current; }
           }
           public void Reset()
           {
               throw new NotSupportedException();
           }
           public void Dispose()
           {
           }
       }
   class MainClass
   {
       static void Main()
       {
           CountingEnumerable counter = new CountingEnumerable();
           foreach (int x in counter)
           {
               Console.WriteLine(x);
           }
       }
   }</source>

Get list of integer whose value is higher than 10

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Xml.Linq;

   class Program
   {
       static void Main(string[] args)
       {
           int[] integers = { 1, 6, 2, 27};
           
           IEnumerable<int> twoDigits = from numbers in integers where numbers >= 10 select numbers;
           Console.WriteLine("Integers > 10:");
           foreach (var number in twoDigits)
           {  
              Console.WriteLine(number);
           }
       }
   }</source>

Implement IEnumerable with loop

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; class MyClass : IEnumerable<string> {

  IEnumerator<string> Letter
  {
     get
     {
        string[] s = { "A", "B", "C" };
        for (int i = 0; i < s.Length; i++)
           yield return s[i];
     }
  }
  public IEnumerator<string> GetEnumerator()
  {
     return Letter;
  }
  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  {
     return Letter;
  }

} class MainClass {

  static void Main()
  {
     MyClass mc1 = new MyClass();
     foreach (string s in mc1)
        Console.Write("{0} ", s);
  }

}</source>

A B C

Implement IEnumerable with "yield"

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; class MyClass : IEnumerable<string> {

  IEnumerator<string> Letter
  {
     get
     {
        yield return "A";
        yield return "B";
        yield return "C";
     }
  }
  public IEnumerator<string> GetEnumerator()
  {
     return Letter;
  }
  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  {
     return Letter;
  }

} class MainClass {

  static void Main()
  {
     MyClass mc1 = new MyClass();
     foreach (string s in mc1)
        Console.Write("{0} ", s);
  }

}</source>

A B C

Iterator Block Iteration Sample

<source lang="csharp">using System; using System.Collections; using System.ruponentModel;

   class IteratorBlockIterationSample : IEnumerable
   {
       object[] values;
       int startingPoint;
       public IteratorBlockIterationSample(object[] values, int startingPoint)
       {
           this.values = values;
           this.startingPoint = startingPoint;
       }
       public IEnumerator GetEnumerator()
       {
           for (int index = 0; index < values.Length; index++)
           {
               yield return values[(index + startingPoint) % values.Length];
           }
       }
       static void Main()
       {
           object[] values = { "a", "b", "c", "d", "e" };
           IteratorBlockIterationSample collection = new IteratorBlockIterationSample(values, 3);
           foreach (object x in collection)
           {
               Console.WriteLine(x);
           }
       }
   }</source>

Provide different IEnumerable implemetation for one class

<source lang="csharp">using System; using System.Collections.Generic; public class Employee {

   public string Name;
   public string Title;
   public Employee(string name, string title)
   {
       Name = name;
       Title = title;
   }
   public override string ToString()
   {
       return string.Format("{0} ({1})", Name, Title);
   }

} public class EmployeeList {

   private List<Employee> employeeList = new List<Employee>();
   public IEnumerator<Employee> GetEnumerator()
   {
       foreach (Employee tm in employeeList)
       {
           yield return tm;
       }
   }
   public IEnumerable<Employee> Reverse
   {
       get
       {
           for (int c = employeeList.Count - 1; c >= 0; c--)
           {
               yield return employeeList[c];
           }
       }
   }
   public IEnumerable<Employee> FirstTwo
   {
       get
       {
           int count = 0;
           foreach (Employee tm in employeeList)
           {
               if (count >= 2)
               {
                   yield break;
               }
               else
               {
                   count++;
                   yield return tm;
               }
           }
       }
   }
   public void AddEmployee(Employee member)
   {
       employeeList.Add(member);
   }

} public class MainClass {

   public static void Main()
   {
       EmployeeList employeeList = new EmployeeList();
       employeeList.AddEmployee(new Employee("A", "AA"));
       employeeList.AddEmployee(new Employee("B", "BB"));
       employeeList.AddEmployee(new Employee("C", "CC"));
       
       Console.WriteLine("Enumerate using default iterator:");
       foreach (Employee member in employeeList)
       {
           Console.WriteLine("  " + member.ToString());
       }
       Console.WriteLine("Enumerate using the FirstTwo iterator:");
       foreach (Employee member in employeeList.FirstTwo)
       {
           Console.WriteLine("  " + member.ToString());
       }
       Console.WriteLine("Enumerate using the Reverse iterator:");
       foreach (Employee member in employeeList.Reverse)
       {
           Console.WriteLine("  " + member.ToString());
       }
   }

}</source>

Enumerate using default iterator:
  A (AA)
  B (BB)
  C (CC)
Enumerate using the FirstTwo iterator:
  A (AA)
  B (BB)
Enumerate using the Reverse iterator:
  C (CC)
  B (BB)
  A (AA)

yield value for IEnumerable

<source lang="csharp">using System; using System.Collections.Generic; public class MainClass {

   static public IEnumerable<int> Powers( int from,
                                          int to ) {
       for( int i = from; i <= to; ++i ) {
           yield return (int) Math.Pow( 2, i );
       }
   }
   static void Main() {
       IEnumerable<int> powers = Powers( 0, 16 );
       foreach( int result in powers ) {
           Console.WriteLine( result );
       }
   }

}</source>

1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536