IEnumerable interface as a type?
While learning to use LINQ in VB.NET, I came across the following:
Dim x As IEnumerable = (some LINQ query)
If you can't instantiate an interface, but only a concrete implementation of it, why is this allowed? Is there some difference between d开发者_如何学Coing Dim x as (Type) and Dim x as New (Type)?
Yes, a big difference. In the code you give, you're declaring a variable that will point at some instantiated thing. It doesn't necessarily know what type of thing it is, but it knows it implements IEnumerable. In the code you gave, the actual instantiation as some concrete type (that implements IEnumerable) is handled by LINQ. The part that goes on the right has to result in a concrete instatiation.
精彩评论