Custom collections - how we are able to get foreach and linq features without implementing any methods ?

Ravi R 1 Reputation point

Using a custom class Customer.
I have not implemented iEumerable.
Also i have not overridden the methods MoveNext(), Reset() of iEnumerator.

But i am able to use foreach to iterate through customers collection. And able to query using linq where clause.

I am not able to understand how we are able to get these foreach and linq features without implementing any methods in the below example.

Please help me to understand how this works.

public class Customer
public string Name { get; set; }
public string City { get; set; }
public string Mobile { get; set; }


// in main method
Customer[] customers = new Customer[]
new Customer {Name="Raj Kumar", City="Bangalore", Mobile="9599999123"},
new Customer { Name = "Sudhir Wadje", City = "Bhubaneswar", Mobile = "9488888321" },
new Customer { Name = "Anil", City = "Delhi", Mobile = "8077777987" }

        IEnumerable<Customer> result = from cust in customers where (cust.City.StartsWith("B")) select cust;
        foreach (Customer customer in result)
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,408 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bonnie DeWitt 811 Reputation points

    It's because your customers variable is an array of Customer. It's the array that has all those IEnumerable methods. It would be the same for a List also.

    3 people found this answer helpful.