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.
Custom collections - how we are able to get foreach and linq features without implementing any methods ?
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)
{
Console.WriteLine(customer.City);
}