Linq Types

I am wondering about what type is returned when we do a Linq select.

Consider the attached code for discussion.

I know that the fortiesPeople in var fortiesPeople = from p in people where p.Age > 40 select p; returns an object which implements IEnumerable.  When I try and see what type it is though I get:

fortiesPeople type = System.Linq.Enumerable+WhereListIterator`1[LinqExamples.Person]

So what is all that - what is the + and the '1 etc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace LinqExamples
    public class Program
        static void Main(string[] args)
            Person dave = new Person { FirstName = "Dave", LastName = "Amour", Age = 42 };
            Person fred = new Person { FirstName = "Fred", LastName = "Smith", Age = 22 };
            Person tiff = new Person { FirstName = "Tiff", LastName = "Bloggs", Age = 24 };
            Person kate = new Person { FirstName = "Kate", LastName = "Brown", Age = 33 };
            Person bill = new Person { FirstName = "Bill", LastName = "Green", Age = 38 };
            Person ian = new Person { FirstName = "Ian", LastName = "Smith", Age = 19 };
            Person fiona = new Person { FirstName = "Fiona", LastName = "Williams", Age = 28 };
            Person sonia = new Person { FirstName = "Sonia", LastName = "Johnson", Age = 43 };
            List<Person> people = new List<Person>();
            var fortiesPeople = from p in people where p.Age > 40 select p;
            Console.WriteLine("fortiesPeople type = " + fortiesPeople.GetType().ToString());
            foreach (Person person in fortiesPeople)
    public class Person
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public override string ToString()
            return FirstName + " " + LastName + " (Age " + Age.ToString() + ")";

Open in new window

LVL 19
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
That is the signature for generics.  The class is defined like this:

private class WhereListIterator<TSource> : Enumerable.Iterator<TSource>

Enumerable = System.Linq.Enumerable
daveamourAuthor Commented:
Ok thanks but what is the + and the '1
Also it says is is System.Linq.Enumerable, not WhereListIterator.
Sorry still clueless.
Bob LearnedCommented:
Any time you see WhereListIterator`1, that means that it uses generics (WhereListIterator<TSource>).  The "+" symbol means that the first class is a base class (System.Linq.Enumerable), and the second class is the deriving class (WhereListIterator).

If you look at the class descriptor with Reflector, as shown above, you can see that WhereListIterator implements Enumerable.Iterator<TSource>, which is from the System.Linq.Enumerable class.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

daveamourAuthor Commented:
So the type is WhereListIterator which has a base class of System.Linq.Enumerable.
Where is the WhereListIterator class - I can't find it anywhere?
Bob LearnedCommented:
private class WhereListIterator

WhereListIterator is a private class in the framework, so you can't see it, but reflection can (I used Reflector to find it).
daveamourAuthor Commented:
Hmm if it's private then how come I can get an instance of it?  Confused.
Bob LearnedCommented:
Please explain, "I can get an instance of it".  Where are you creating an instance of a WhereListIterator?
daveamourAuthor Commented:
I am trying to find out what fortiesPeople is below
var fortiesPeople = from p in people where p.Age > 40 select p;
Did you not say it was a WhereListIterator or have I misunderstood?
Bob LearnedCommented:
"var fortiesPeople" uses type inference, which builds instances inside of the framework, that you can use to enumerate.  You are not creating an instance, the CLR is.  When you are checking the type, the CLR tells you what type it created.
daveamourAuthor Commented:
Ok so what type is it then?
Bob LearnedCommented:
WhereListIterator, which inherits from System.Linq.Enumerable, and lets you enumerate the objects.
daveamourAuthor Commented:
I don't understand how I can have an instance of a class (created by the CLR) which is of a private type.  Is it in fact expressed as an interface  - ie IEnumerbable - ie is there a method somewhere like this
public IEnumerable MyMethod()
Bob LearnedCommented:
Yes, you are getting that through an interface (IEnumerable<TSource>, IEnumerable, IEnumerator<TSource> or IEnumerator):

private abstract class Iterator<TSource> : IEnumerable<TSource>, IEnumerable, IEnumerator<TSource>, IDisposable, IEnumerator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.