Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using IEnumerator and using an array as an property

I get the idea of IEnumerator which gives an abilty of iteration to a class. I get the idea of why GetEnumerable() is needed to use foreach statement. But I wonder why I need to implement IEnumerator while I can declare an array as an property? I am really confused and could use a good explanation. Here is my codeblock, the result is the same with and without IEnumerator implementation.

static void Main(string[] args)
    {

        Customer customer1 = new Customer
        {
            Name = "Marty",
            Surname = "Bird",
            Id = 1
        };
        Customer customer2 = new Customer
        {
            Name = "Hellen",
            Surname = "Pierce",
            Id = 2
        };
        Customers customers = new Customers();
        customers.Add(customer1);
        customers.Add(customer2);
        //Using a property: This one works without an extra GetEnumerator implementation.
        foreach (var item in customers.CustomerList)
        {
            Console.WriteLine(item.Name);
        }
        //Using with GetEnumerator()
        foreach (Customer item in customers)
        {
            Console.WriteLine(item.Name);
        }

    }
    class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

    class Customers:IEnumerable
    {
        private List<Customer> customerList = new List<Customer>();

        public List<Customer> CustomerList { get=> customerList; }
        public void Add(Customer p)
        {
            customerList.Add(p);
        }

        public IEnumerator GetEnumerator()
        {
            return customerList.GetEnumerator();
        }
    }
like image 904
Muhammet Topcu Avatar asked Apr 08 '26 17:04

Muhammet Topcu


1 Answers

You really don't need to implement IEnumerator at all. You can just get rid of the Customers class and replace the line:

Customers customers = new Customers();

with this:

List<Customer> customers = new List<Customers>();

The only reason you were required to implement GetEnumerator was because you had inherited the class Customers from IEnumerable, which is an interface that declares that method. All methods and properties in an interface must be implemented in a class that implements that interface.

Just given the code you've shown you don't need the Customers class, just use a List as I explained above.

However, if you really need a Customers class, because you plan to add more functionality to it than what you've shown, one option would be to inherit from List<Customer>. Then it would work in the foreach statement without needing to implement GetEnumerable() because List already implements that method.

like image 128
ososnilknarf Avatar answered Apr 11 '26 07:04

ososnilknarf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!