Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, was just learning about IEnumerable interface and found that we can iterate through generic as well as non-generic collection with the help of IEnumerable interface, but then just to check i created a class Customer with some attributes.Then in Main method created a array of that Class adding few objects.Then ran through foreach loop and worked fine without using IEnumerable Interface.Can u explain, may be my question would be weird or foolish, but to be onest not able to understand exactly what is IEnumerable and when and why to use IEnumerable interface.Thanks in advance if anyone can explain in Detail.

public class Customer1
    {
        private String _Name, _City;
        private long _Mobile;
        private double _Amount;

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public String City
        {
            get { return _City; }
            set { _City = value; }
        }

        public long Mobile
        {
            get { return _Mobile; }
            set { _Mobile = value; }
        }
        
        public double Amount
        {
            get { return _Amount; }
            set { _Amount = value; }
        }
    }
    class IEnumerableTest
    {
        static void Main()
        {
            Customer1[] customers = new Customer1[]
            {
             new Customer1 {Name="Vithal Wadje",City="Mumbai",Mobile=99999999999,Amount=89.45 },
             new Customer1 { Name = "Sudhir Wadje", City = "Latur", Mobile = 8888888888888888888, Amount =426.00 },
             new Customer1 { Name = "Anil", City = "Delhi", Mobile = 7777777777777777777, Amount = 5896.20 }
            };

            foreach (var obj in customers)
            {
                Console.WriteLine(obj.Name + " " + obj.City + " " + obj.Mobile + " " + obj.Amount);
            }
        }
    }

What I have tried:

I have  created a class Customer with some attributes.Then in Main method created a array of that Class adding few objects.Then ran through foreach loop and worked fine without using IEnumerable Interface.
Posted
Updated 7-Mar-19 8:10am

1 solution

Customer1 does not implement IEnumerable, but it doesn't have to that in code:
Customer1[] customers = new Customer1[] { ... };
foreach (var obj in customers)
   ...
because all Array (and other collections) do implement IEnumerable.

Interfaces are a contract, they say "it you do this and that, you are a member of the club, and get all club privileges". The "this and that" are the properties, methods, and so forth that are defined to be part of the interface.
In the case of IEnumerable, you just have to implement the following method:
public IEnumerator GetEnumerator()
    {
    ...
    }
And How to implement IEnumerable in C#[^] will show you how to do that.
If you do, then your class can be directly used as the foreach "collection":
foreach (MyClass mc in MyClassThatImplementsIEnumerable)
   {
   ...
   }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900