IEnumerable: what does this mean in the context of OOP

Pay attention to the following code:

public class Person ( public string FirstName {get; set;} public string LastName {get; set;} Public int Age {get; set;} } IEnumerable <Person> people; 

I also saw in many programs something like <IQueryable> , what does this mean?

What is the meaning of IEnumerable<Person> here?

+6
ienumerable
source share
4 answers

IEnumerable<T>

There are many ways that many things can be stored in a list, in an array, in a database, etc. One thing that has common storage mechanisms is that the set of things that can be stored can be Enumerated , each of which can be accessed one by one using a language construct like foreach .

The IEnumerable<T> interface is a common interface that all owners of sets of things must include an enumeration. Therefore, IEnumberable<Person is an interface that allows you to list objects from Person without specific information about the actual way that these objects are stored.

IQueryable<T>

This is an extension for IEnumerable<T> , but it is something specific to LINQ. In LINQ, you can specify a query that can filter and transform a set (or set) of elements. Against the pure IEnumerable<T> interface, this will be known as LINQ to Objects, in which each step of the query is ultimately a simple extension method call.

However, against an object that implements IQueryable<T> , things can be more complex. An object that implements IQueryable<T> can be associated with another LINQ Provider , which can implement the execution of LINQ queries differently. For example, IQueryable<Person> may be implemented by a type associated with the LINQ-To-SQL provider. In this case, the LINQ query is converted to a T-SQL query against the database to obtain a set of elements (the result may not necessarily be a set of Person objects).

+4
source share

IEnumerable means nothing in the context of OOP.

IEnumerable and IEnumerable<T> are .NET interfaces that simply indicate that a sequence can be enumerated. Classes that implement IEnumerable or IEnumerable<T> provide properties / methods to make this enumeration possible.

For example:

 IEnumerable<Person> people = GetPeopleFromSomewhere(); // iterate through the sequence foreach (Person p in people) { Console.WriteLine(p.Name); } // use LINQ to query the sequence var peopleOver30 = people.Where(p => p.Age > 30); 
+3
source share

IQueryable extends IEnumerable and is intended to be implemented by LINQ providers. You can fulfill requests to any provider, that is, abstract data providers through this interface. It extends IEnumerable so that query results can be repeated. for example a loop over all Person objects using LastName.equals ("Smith")

IEnumerable<People> means that the variable refers to an object that will allow you to iterate / enumerate / loop over a collection of Person objects

 foreach(Person person in people) Console.WriteLine(person.ToString()); 

It's a little hard to explain (John Skeet does a great job of this in his book). The type that implements IEnumerable basically declares that I can provide you with an iterator object (via the GetEnumerator interface method) that displays the IEnumerator interface, which you can use to access the current item in the collection and MoveNext to go to the next item in the list. the foreach construct internally invokes the above operations to achieve its goal.
See the example on MSDN . Introduce the iterator yourself, and this will become more clear.

0
source share

IEnumerable<T> is a type of .NET platform. It simply represents an enumerable sequence of elements without any sequence management methods. Many types implement this interface โ€” for example, ICollection<T> , IList<T> and BindingList<T> it is usually used instead of a specific type of field or variable if the sequence is to be considered read-only.

0
source share

All Articles