2014-01-17 75 views
-3

我的问题是如果IEnumberable是接口那么为什么.Net允许实例化IEnumerable实例?接口实例

IEnumerable <string> Errors= new IEnumerable<string>() 

为什么我们没有像他们那样实例化我们自己的接口?

+0

哪里.NET实例化的界面,你应该得到这个错误?你确定它不只是公开一个实现了接口的对象作为该接口吗? –

+0

'错误:无法创建抽象类或接口的实例'System.Collections.Generic.IEnumerable ''您能给我们一些实际编译的代码的例子吗? –

回答

2

你不能创建接口的实例,你给定的代码会给你带来错误。

Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IEnumerable'

是的,你可以初始化类的接口变量,它是实现它:

IEnumerable <string> Errors = new List<string>(); 
0

Why .Net allow to instantiate IEnumerable instance

那么你能做到这一点?

IEnumerable <string> Errors= new IEnumerable<string>() 

当您尝试做

Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IEnumerable'