Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#接口作为数据类型_C# - Fatal编程技术网

C#接口作为数据类型

C#接口作为数据类型,c#,C#,嗨,伙计们,我正在努力学习一些界面。我得到了关于它的基本知识,那是一个班级的合同。我现在对接口作为一种数据类型有点困惑 问题: 将接口用作数据类型的目的是什么 前 或 如果im正确,IEnumerable也是一个接口。 值类型保存在何处(如果有) Thx这意味着数据类型可以引用实现接口的类的任何对象 IEnumerable thing=new List()工作 IEnumerable thing=new Collection()也可以工作 IEnumerable thing=new HashSe

嗨,伙计们,我正在努力学习一些界面。我得到了关于它的基本知识,那是一个班级的合同。我现在对接口作为一种数据类型有点困惑

问题: 将接口用作数据类型的目的是什么

如果im正确,IEnumerable也是一个接口。 值类型保存在何处(如果有)


Thx

这意味着数据类型可以引用实现接口的类的任何对象

IEnumerable thing=new List()工作

IEnumerable thing=new Collection()也可以工作

IEnumerable thing=new HashSet()也可以工作

在任何情况下,您不需要知道实际分配给对象的确切类型,您只需要实现契约的任何类型

例如,所有
IEnumerator
实现
GetEnumerator()
,允许您使用
foreach


foreach(事物中的变量元素)
将运行任何类型的事物,它只需要实现IEnumerator。

这将为您提供一个好主意-
IEnumerable Folders {get;} 
IComparable Compare {get;}