IDictionary 接口的用途

发布于 2024-07-21 13:46:51 字数 194 浏览 8 评论 0原文

需要什么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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

羁拥 2024-07-28 13:46:51

它定义了字典应实现的重要函数

MSDN 中的行意味着您正在创建一个对象 openWith,它实现 IDictionary 接口中定义的函数(方法)。

当您使用 Dictionary 来声明变量时,如下所示:

Dictionary<string,string> openWith=.....;

您与对象的具体类型绑定。 但是当你使用时,

IDictionary<string,string> openWith=....;

你可以将它与任何实现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:

Dictionary<string,string> openWith=.....;

you are bind with the concrete type of object. But when you use

IDictionary<string,string> openWith=....;

you can use it with any object that implements IDictionary interface, maybe your own custom class :)

弃爱 2024-07-28 13:46:51

我怀疑您只是忽略了类型为 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 a Dictionary<,> (note no I; the concrete type).

叶落知秋 2024-07-28 13:46:51

它对于单元测试也很有用。 您可以为接受 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).

哆啦不做梦 2024-07-28 13:46:51

它与任何其他界面没有什么不同。 尝试考虑一个更简单的例子:

interface IThermometer
{
    double CurrentTemperature { get; }
}

现在我们有一种方法来获取温度,尽管我们并不关心它是如何测量的。 我们可以创建各种实现:

class MercuryThermometer : IThermometer
{
    public double CurrentTemperature
    {
        get { return ... /* gets the temperature somehow */ }
    }
}

程序的其余部分不需要知道它正在使用哪个温度计。

It would be no different to any other interface. Try thinking about a simpler example:

interface IThermometer
{
    double CurrentTemperature { get; }
}

Now we have a way to get the temperature, though we don't care exactly how it's measured. We can create various implementations:

class MercuryThermometer : IThermometer
{
    public double CurrentTemperature
    {
        get { return ... /* gets the temperature somehow */ }
    }
}

The rest of the program doesn't need to know which thermometer it's using.

橘寄 2024-07-28 13:46:51

接口的全部要点是提供......好吧,任何模块的接口(我在这里使用广义上的“模块”),这样调用代码就不必担心这个特定接口是如何实现的。

至于“IDictionary接口如何初始化”,这在技术上是不正确的。 可以初始化的是一个变量,其类型为IDictionary。 当然,变量必须被初始化,但这通常对“客户端代码”是隐藏的。

然而,IDictionary 并不是很有代表性。 相反,请考虑使用 IDataReader 接口。 您肯定已经处理过 ADO.NET,所以这应该看起来很熟悉:

public Foo PopulateFromDataReader(SqlDataReader dataReader)

这个特定的方法与 SqlDataReader 紧密耦合,因此您必须重写它才能支持 Access 或Oracle 或 MySQL 或 Firebird 或其他什么。 换句话说,你依赖于实施。

现在考虑一下:

public Foo PopulateFromDataReader(IDataReader dataReader)

此方法可以与任何实现 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 is IDictionary<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 an IDataReader interface. You've surely dealt with ADO.NET, so this should look familiar:

public Foo PopulateFromDataReader(SqlDataReader dataReader)

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:

public Foo PopulateFromDataReader(IDataReader dataReader)

This method can be used with whatever class that implements IDataReader, which means with basically any ADO.NET-compatible data provider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文