This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23666|Reply: 1

[Source] A few simple uses of IEnumerable

[Copy link]
Posted on 5/9/2015 6:07:14 PM | | |
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...




Previous:The type "System.Data.Linq.DataContext" is defined in the unreferenced assembly. Must add a response to the program...
Next:Solve the problem of linq return value being empty or forcing the type to be null
Posted on 5/12/2015 12:57:42 PM |

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com