开发者

IEnumerable interface as a type?

开发者 https://www.devze.com 2023-02-03 21:34 出处:网络
While learning to use LINQ in VB.NET, I came across the following: Dim x As IEnumerable = (some LINQ query)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号