LINQ returns interface?

Just by looking at the LINQ tutorials on MSDN and move on to the following code:

IEnumerable<int> numQuery1 = 
            from num in numbers
            where num % 2 == 0
            orderby num
            select num;

I do not understand how the interface returns? I realized that the interface is just an outline for the class and, of course, cannot contain any data.

I would like this to be clarified.

+5
source share
7 answers

- : Linq , ( ), , IEnumerable<int>, . List<int> WhackyLinqInternalEnumerable<int>, , , , , , ( ).

, - , : (reference) "" . , . , , , , , ( ), .

+10

... ,

: :

interface IA 
{
   void methodA();
}

Class B : IA
{

}

Class C
{
   IA a = new B();
}
0
public interface IMyInterface {
}

public class MyCoolInterfaceImplementation : IMyInterface  {
}

//somewhere in code 

IMyInterface inter = new MyCoolInterfaceImplementation (); 

. , .

0

, linq , . , , , , , .

0

, , , .

"", . : " , ".

LINQ , IEnumerable<int>.

, :

Debug.WriteLine(numQuery1.GetType().FullName);

.

:

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

.NET 4.0:

System.Linq.OrderedEnumerable`2 [[System.Int32, mscorlib, Version = 4.0.0.0, Culture = , PublicKeyToken = b77a5c561934e089], [System.Int32, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]]

, System.Linq.OrderedEnumerable<int, int>.

, , . , , , LINQ , . .NET - , , IEnumerable<T>.

0

Think of it this way: it's 5 hours and you want to go home. At the moment, it does not matter how you are going to get there: (abstractly speaking) you can create a Car class or a Bicycle class or even a Tank class . But honestly, do you care? You simply take a copy of any IVehicle tools and simply return home.

0
source

All Articles