views:

155

answers:

4

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 ?

+2  A: 

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);
LukeH
A: 

IQueryable extends IEnumerable and is intended to be implemented by LINQ providers. You can execute queries against any provider - i.e. abstract away diff providers via this interface. It extends IEnumerable so that the results of the Query can be iterated over. e.g. loop over all Person objects with 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 bit complicated to explain (Jon Skeet does a great job at this in his book). A type implementing IEnumerable basically declares that I can give you an iterator object (via the GetEnumerator interface method) exposing an IEnumerator interface, which you can use to access the Current element in the collection and MoveNext to move to the next element in the list. the foreach construct internally calls the above operations to achieve its purpose.
See the example in MSDN, Implement an iterator yourself and it'll be more clear.

Gishu
A: 

IEnumerable<T> is a .NET framework type. It just represents a enumerable sequence of elements without any methods for manipulating the sequence. Many types implement this interface - for example ICollection<T>, IList<T>, and BindingList<T> - and it is commonly used instead of the concrete type of a field or variable if the sequence should be treated as read-only.

Daniel Brückner
A: 

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).

AnthonyWJones
One of the best answers in plain simple english
Edwards