IDictionary 接口的用途
需要什么IDictionary接口。 如何初始化 IDictionary 接口。 毕竟它只是一个接口。 以下代码片段来自msdn。 我无法理解。
IDictionary<string, string> openWith = new Dictionary<string, string>();
What is the need of IDictionary interface. How can IDictionary interface be initialized. After all it is just an interface. The following code snippet is from msdn. I could not understand it.
IDictionary<string, string> openWith = new Dictionary<string, string>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它定义了字典应实现的重要函数。
MSDN 中的行意味着您正在创建一个对象 openWith,它实现 IDictionary 接口中定义的函数(方法)。
当您使用 Dictionary 来声明变量时,如下所示:
您与对象的具体类型绑定。 但是当你使用时,
你可以将它与任何实现IDictionary接口的对象一起使用,也许是你自己的自定义类:)
It defines the important functions that an Dictionary should implement.
The line from MSDN means that you are creating an object openWith which implements the functions (methods) defined in IDictionary interface.
When you use Dictionary to declare the variable like:
you are bind with the concrete type of object. But when you use
you can use it with any object that implements IDictionary interface, maybe your own custom class :)
我怀疑您只是忽略了类型为
IDictionary<,>
的变量(接口)和初始化为Dictionary<,>
的值(引用)之间的区别。 code> (注意没有I
;具体类型)。I suspect you've simply overlooked the difference between the variable typed as
IDictionary<,>
(the interface), and the value (reference) initialized as aDictionary<,>
(note noI
; the concrete type).它对于单元测试也很有用。 您可以为接受 IDictionary 而不是 Dictionary 的方法编写单元测试并传递模拟。 如果它接受一个类实例(也可以被密封),你就会有点搞砸了(你必须使用适配器模式等等)。
It's also useful for unit testing. You can write a unit test for a method that accepts an IDictionary instead of a Dictionary and pass a mock. If it were to accept a class instance (which could also be sealed) you'd be a little screwed (you'd have to use the adapter pattern and so on).
它与任何其他界面没有什么不同。 尝试考虑一个更简单的例子:
现在我们有一种方法来获取温度,尽管我们并不关心它是如何测量的。 我们可以创建各种实现:
程序的其余部分不需要知道它正在使用哪个温度计。
It would be no different to any other interface. Try thinking about a simpler example:
Now we have a way to get the temperature, though we don't care exactly how it's measured. We can create various implementations:
The rest of the program doesn't need to know which thermometer it's using.
接口的全部要点是提供......好吧,任何模块的接口(我在这里使用广义上的“模块”),这样调用代码就不必担心这个特定接口是如何实现的。
至于“
IDictionary
接口如何初始化”,这在技术上是不正确的。 可以初始化的是一个变量,其类型为IDictionary
。 当然,变量必须被初始化,但这通常对“客户端代码”是隐藏的。然而,
IDictionary
并不是很有代表性。 相反,请考虑使用IDataReader
接口。 您肯定已经处理过 ADO.NET,所以这应该看起来很熟悉:这个特定的方法与
SqlDataReader
紧密耦合,因此您必须重写它才能支持 Access 或Oracle 或 MySQL 或 Firebird 或其他什么。 换句话说,你依赖于实施。现在考虑一下:
此方法可以与任何实现 IDataReader 的类一起使用,这意味着基本上可以与任何 ADO.NET 兼容的数据提供程序一起使用。
The whole point of interfaces is to provide... well, an interface to whatever module (I use "module" in a broad sense here) so that calling code will not have to worry about how this particular interface is implemented.
As for "How can
IDictionary
interface be initialized", this is technically not correct. What can be initialized is a variable, whose type isIDictionary<T, V>
. Sure enough variables have to be initialized, but that's usually hidden from the "client code".IDictionary
is not very representative, however. Rather, consider anIDataReader
interface. You've surely dealt with ADO.NET, so this should look familiar:This particular method is tightly coupled to an
SqlDataReader
, so you'd have to rewrite it for it to support, say, Access or Oracle or MySQL or Firebird or whatever. In other words, you depend on implementation.Now consider:
This method can be used with whatever class that implements
IDataReader
, which means with basically any ADO.NET-compatible data provider.