Linq, and how to determine if IEnumerable<T>

G

GiJeet

Hello, I know that all that’s required to use linq to objects, is the
collection must implement IEnumerable<T> but how to know if a
collection implements this interface? For example one of the books
I’m reading uses the running system processes and I wouldn't know off
the top of my head if these processes implement the IEnumerable<T>
interface so is there a quick way to know if some objects implement
this interface without writing code to test it?

thx
G
 
J

Jon Skeet [C# MVP]

GiJeet said:
Hello, I know that all that=3Fs required to use linq to objects, is the
collection must implement IEnumerable<T> but how to know if a
collection implements this interface? For example one of the books
I=3Fm reading uses the running system processes and I wouldn't know off
the top of my head if these processes implement the IEnumerable<T>
interface so is there a quick way to know if some objects implement
this interface without writing code to test it?

Pretty much every collection implements at least IEnumerable. For any
particular collection though, just check the documentation.

I wouldn't expect a single process to implement IEnumerable<T>, but the
*collection* of running processes almost certainly will.
 
G

GiJeet

but the *collection* of running processes almost certainly will.

So, just to make sure I have this correct, it’s always the collection
we create to hold our objects, whether we create a List<T> or Array or
Collection<T> or even IEnumerable<T> that needs to implement this
interface and it doesn’t matter what objects we insert into the
collect (system processes, strings, ints, cars, etc), because it’s the
collect we enumerate over. Do I have this right?

G
 
G

GiJeet

Are we always querying against a collection (in Linq-to-objects)? I
hear the term "queryable types", is this referring to just collections
or ANY object that implements IEnumerable? So, I guess I'm still not
clear on exactly what we can query against...objects? collections?
both?

G
 
J

Jon Skeet [C# MVP]

GiJeet said:
So, just to make sure I have this correct, it=3Fs always the collection
we create to hold our objects, whether we create a List<T> or Array or
Collection<T> or even IEnumerable<T> that needs to implement this
interface and it doesn=3Ft matter what objects we insert into the
collect (system processes, strings, ints, cars, etc), because it=3Fs the
collect we enumerate over. Do I have this right?

Yes.
 
J

Jon Skeet [C# MVP]

GiJeet said:
Are we always querying against a collection (in Linq-to-objects)?

Well, querying against "something that implements IEnumerable".
I hear the term "queryable types", is this referring to just collections
or ANY object that implements IEnumerable? So, I guess I'm still not
clear on exactly what we can query against...objects? collections?
both?

Anything that implements IEnumerable/IEnumerable<T>. (Most of LINQ to
Objects requires IEnumerable<T>, but you can use Cast/OfType to obtain
an IEnumerable<T> from an IEnumerable.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top