Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable: Whats does it mean in the context of OOP

Please consider the following code:

public class Person
(
    public string FirstName {get; set;}
    public string LastName  {get; set;}
    Public int Age  {get; set;}
}

IEnumerable <Person> people;

Also i have seen in many programs something like <IQueryable> what does that mean ?

What is the meaning of IEnumerable<Person> here ?

like image 729
Edwards Avatar asked Sep 01 '09 09:09

Edwards


People also ask

What are the 4 basics of OOP?

Abstraction, encapsulation, inheritance, and polymorphism are four of the main principles of object-oriented programming.

What defines an OOP?

Object-oriented programming (OOP) is a style of programming characterized by the identification of classes of objects closely linked with the methods (functions) with which they are associated. It also includes ideas of inheritance of attributes and methods.

What is oops concept in C#?

C# - What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

What is class and object in oops?

A Class in object oriented programming is a blueprint or prototype that defines the variables and the methods (functions) common to all Java Objects of a certain kind. An object in OOPS is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.


2 Answers

IEnumerable<T>

There many different ways that a set of things can be stored, in a List, in an Array, in a database etc. One thing that these storage mechanisms have in common is that the set of things being held can be Enumerated, that is each one can be accessed one after the other using a language construct such as foreach for example.

The IEnumerable<T> interface represents that common interface that all holders of sets of things have to enable enumeration. Hence IEnumberable<Person represents an interface that allows enumeration of a Set of Person objects without being specific about the actual manner in which those objects are stored.

IQueryable<T>

This is an extension to IEnumerable<T> but represents something specific to LINQ. In LINQ you can specify a query that can filter and transform a set (or sets) of items. Against a purely IEnumerable<T> interface this would be known as LINQ to Objects, in which each stage in the query is ultimately a simple call to an extension method.

However against an object that implements IQueryable<T> things can be more sophisticated. The object implementing IQueryable<T> can be associated with a different LINQ Provider which may implement the execution of LINQ queries in a different way. For example IQueryable<Person> may be implemented by a type associated with the LINQ-To-SQL provider. In this case a LINQ query is transformed to a T-SQL query an executed against a database to get a set of items (the result may not necessarily be a set of Person objects).

like image 102
AnthonyWJones Avatar answered Oct 13 '22 10:10

AnthonyWJones


IEnumerable doesn't mean anything 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 that 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);
like image 39
LukeH Avatar answered Oct 13 '22 11:10

LukeH