Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

Foreach On IEnumerable

Rate me:
Please Sign up or sign in to vote.
4.74/5 (15 votes)
6 Aug 2013CPOL4 min read 175.4K   439   28  
Working of foreach loop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ForeachOnIEnumerable
{
    class Program
    {
      
        static void Main(string[] args)
        {
            var employees = new Employees();
            foreach (Employee employee in employees)
            {
               Console.WriteLine("Employee Id:- "+employee.Employee_Id.ToString()
                                  +"\t\t"
                                  +"Employee Name:- "+employee.Employee_Name);
           
            }

            Console.ReadLine();
        }
    }

    public class Employee
    {
        private int employee_Id;
        private string employee_Name;

        public Employee(int employee_Id, string employee_Name)
        {
            this.employee_Id = employee_Id;
            this.employee_Name = employee_Name;
        }

        public int Employee_Id { get { 
                                        return employee_Id; 
                                     }
                                 set {
                                       employee_Id=value; 
                                     } 
                                }

        public string Employee_Name {
                                        get {
                                              return employee_Name; 
                                            } 
                                        set { 
                                             employee_Name=value; 
                                            } 
                                    }
    }

    public class Employees : IEnumerable,IEnumerator
    {
        public Employee[] employeeList;
        int position = -1;

        public Employees()
        {
            employeeList = new Employee[4]{
                        new Employee(1,"Amey"),
                        new Employee(2,"Pushkar"),
                        new Employee(3,"Raju"),
                        new Employee(5,"Vijay")
            };
        }


        public object Current
        {
            get
            {
                try
                {
                    return employeeList[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public bool MoveNext()
        {
            position++;
            return (position < employeeList.Length);
        }

        public void Reset()
        {
            position = 0;
        }

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Lionbridge
India India
Amey K Bhatkar, a “Microsoft .Net” Web Developer.
I am programmer by will and profession.
I have completed my MCA in 2011 and join software industry.
Presently I am working with Lion Bridge Technologies in Mumbai - India

Comments and Discussions