LINQ returns an interface?
Just looking through th开发者_运维问答e LINQ tutorials on MSDN and come accross the following code:
IEnumerable<int> numQuery1 =
from num in numbers
where num % 2 == 0
orderby num
select num;
I don't understand how an interface is being returned? It was my understanding that an interface is simply an outline for a class, and certainly cannot contain any data.
I would love this to be cleared up.
The interface is a contract on the return value: Linq won't actually tell you what it will return (as in the actual class), but it will guarantee that it behaves like an IEnumerable<int>
, that is, the class it returns implements this interface. It could be a List<int>
or a WhackyLinqInternalEnumerable<int>
- but you don't really care, because, well, all you're interested in is the interface (what you can do with the object).
One thing that might be confusing you is the difference between a variable and the object it represents: For (reference) types, the variable just "points" to the object. You can have a lot of variables "pointing" to the same object. They don't need to have the same type, but the object they all point to must be either of the same type as the variable, a subtype or (in the case of a variable with an interface type) must implement the same variable.
You get an implementation of an interface, but work with it through the Interface - you dont know the concrete Type.
http://en.wikipedia.org/wiki/Interface_(computing)
But you can create the object of type interface... it's possible dont be confuse between class type and interface type
Below is the example : how you can create the type of interface : Same thing happens in above case
interface IA
{
void methodA();
}
Class B : IA
{
}
Class C
{
IA a = new B();
}
public interface IMyInterface {
}
public class MyCoolInterfaceImplementation : IMyInterface {
}
//somewhere in code
IMyInterface inter = new MyCoolInterfaceImplementation ();
This is perfectly valid code. The same idea is in code you provided.
What it means is that the linq query returns an instance of a class that satisfies this interface. You, the caller, do not need to know what the actual class is, only that it satisfies the specified interface.
If a method returns an interface, of course it has to return an actual object, of a class that implements the particular interface.
This is typically considered as the "contract" the method has with the caller. The method says "I will always return something that has this particular behavior".
In this particular case, the LINQ query code has to return any object that implements the IEnumerable<int>
interface.
In other words, if you do this:
Debug.WriteLine(numQuery1.GetType().FullName);
you will see the actual type being returned.
The following example:
var numbers = new[] { 1, 2, 3, 4 };
IEnumerable<int> numQuery1 =
from num in numbers
where num % 2 == 0
orderby num
select num;
Debug.WriteLine(numQuery1.GetType().FullName);
will output this in .NET 4.0:
System.Linq.OrderedEnumerable`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Which indicates that this is System.Linq.OrderedEnumerable<int, int>
.
However, and this is the real point of this, you should not care all that much. All you need to know is that the LINQ query returns something that implements this interface. In a future .NET version, the code might actually return something else, but still an object that implements IEnumerable<T>
.
Think of it like this: it's 5 o'clock and you want to go home. At this point it really doesn't matter how are you going to get there: (abstractly speaking) you can instantiate class Car or class Bike or even class Tank. But honestly, do you care? You just take an instance of whatever implements IVehicle and simply go home.
精彩评论