Why do we see the IEnumerable interface, we may think it is amazing, in general programming, basically we can't think of using it, but as the saying goes, existence is the truth, so what wonderful things can it bring to us? To understand it, let's take a look at its definition! On MSDN, as it is said, it is a public enum that supports simple iterations on non-generic collections. In other words, for all array traversals, from IEnumerable, then we can use this feature to define a common method that can traverse arrays. For example: public static void Print(IEnumerable myList) { int i = 0; foreach (Object obj in myList) { if (obj is Student)// This is the judgment of type, where the student is a class or structure { Student s=(Student)obj; Console.WriteLine("\t[{0}]:\t{1}", i++, s.Sname); } if (obj is int) { Console.WriteLine("INT:{0}",obj); } } Console.WriteLine(); } Above, we can perform multiple if judgments in foreach to perform corresponding operations. Another use of IEnumerable is in generics. Querying in an array with a lamda expression is as follows: List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" }; // List<string> query = fruits. Where(fruit => fruit. Length < 6). ToList(); IEnumerable<string> query = fruits. Where(fruit => fruit. Length < 6); foreach (string fruit in query) Console.WriteLine(fruit); As for the above two examples, I think they are still often used in ordinary programming, we might as well try... |