是Ienumerable是一个类还是一个接口 [英] Is the Ienumerable a class or an interface

查看:91
本文介绍了是Ienumerable是一个类还是一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者c#开发人员。我最近从php转换为c#。根据我之前的知识,以及我在学习c#时获得的知识。我知道接口包含方法定义和其他属性定义。但我经常看到人们像类一样使用Ienumerable接口。这真让我困惑。我不知道我是否学错了,或者我没有注意,或者我发疯了。有人请举个例子,请你解释一下。

I am a beginner c# developer. I converted from php to c# recently. From my previous knowledge, and the one I acquired while learning c#.. I know interface contain method definitions and other property definitions. But I constantly see people using the Ienumerable interface like a class. This is really confusing me. I don't know whether I learnt wrong, or I didn't pay attention or I am going mad. somebody please with an example, can you please explain it to me.

推荐答案

请看我对这个问题的评论。是的,这是界面,不,人们不会把它当成一个类,这是不可能的。



我的想法是你只觉得有些使用它作为一个类,因为您不知道如何使用接口以及继承如何工作。变量或成员可以具有不同的运行时类型编译时类型。如果实现了某个接口,则运行时类型始终为类或 struct 。但是,声明的(编译时)类型可以是接口类型,因为由于继承,它与实现接口的类型赋值兼容



我认为这已经足够了。如果有必要,请随意提出一些后续问题,但请记住:如果此时有些不清楚,最好的想法是从一开始就在OOP 上阅读。只有当您通过示例获得所有主要概念时,您才能理解:

https://msdn.microsoft.com/en-us/library/ms173156.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/hh165754%28v=nav.70%29.aspx [ ^ ]。



-SA
Please see my comment to the question. Yes, this is interface, and no, people don't use it as a class, it's impossible.

My idea is that you only think that some "use it as a class", because you don't know how interfaces are used and how inheritance works. A variable or a member can have different runtime type and compile-time type. If some interface is implemented, the runtime type is always a class or struct. However, the declared (compile-time) type can be the interface type, because it is, due to inheritance, is assignment-compatible with the type implementing the interface.

I think this is enough. Feel free to ask some follow-up questions, if you have to, but keep in mind: if something is unclear at this point, the best idea would be reading on OOP from the very beginning. Only when you get all major concepts, with examples, you can understand this:
https://msdn.microsoft.com/en-us/library/ms173156.aspx[^],
https://msdn.microsoft.com/en-us/library/hh165754%28v=nav.70%29.aspx[^].

—SA


这篇关于是Ienumerable是一个类还是一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆