C# Program to Implement for-each in Interface

This is a C# Program to implement for each in interface.

Problem Description

This C# Program Implements for-each in Inteface.

Problem Solution

It demonstrates the best practice for iterating a custom collection by implementing the IEnumerable and IEnumerator interfaces. In this code, members of these interfaces are not explicitly called, but they are implemented to support the use of foreach to iterate through the collection.

Program/Source Code

Here is source code of the C# Program to Implement for-each in Inteface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement for-each in Inteface
 */
using System;
using System.Collections;
class GrowableArray : IEnumerable
{
  object[] a;
  public GrowableArray(int size) 
  {
    a = new object[size];
  }
  public GrowableArray() : this(8) {}
  void Grow() 
  {
    object[] b = new object[2 * a.Length];
    Array.Copy(a, b, a.Length);
    a = b;
  }
  public object this[int i] 
  {
    set 
    {
      if (i >= a.Length) Grow();
      a[i] = value;
    }
    get 
    {
      if (i >= a.Length) Grow();
      return a[i];
    }
  }
  public IEnumerator GetEnumerator() 
  {
    return new GAEnumerator(a);
  }
  class GAEnumerator : IEnumerator 
  {
    object[] a;
    int i = -1;
    public GAEnumerator(object[] a) { this.a = a; }
    public object Current 
   { 
      get 
      { 
         return a[i]; 
      }
   }
    public void Reset() 
    { 
        i = -1; 
    }
    public bool MoveNext() 
    { 
      do i++; 
      while (i < a.Length && a[i] == null);
      if (i == a.Length) 
         return false; 
      else return true;
    }
  }
}
class Test 
{
public static void Main() 
{
    GrowableArray a = new GrowableArray(2);
    a[0] = 0;
    a[1] = 1;
    a[3] = 3;
    foreach (object x in a) Console.Write(" " + x);
}
}
Program Explanation

This C# program is used to implement for-each in Interface. It demonstrates the best practice for iterating a custom collection by implementing the IEnumerable and IEnumerator interfaces.

advertisement
advertisement

In this code, members of these interfaces are not explicitly called, but they are implemented to support the use of foreach to iterate through the collection.

Runtime Test Cases
 
Demonstrating foreach Interface by Displaying Numbers from 100 to 105 : 
100
101
102
103
104
105

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.