开发者

What is the difference between dictionary(Of String, String) and IDictionary(Of String, String)

Can I do anything more or less with IDi开发者_Python百科ctionary? How do these two collections differ?


IDictionary is only an interface - a contract which describes what an implementing class must do. Dictionary is a concrete class that implements that interface and hence must provide the methods described in the IDictionary interface.

If you want to create a dictionary you must create a concrete class (i.e. Dictionary), but if all you want to do with that dictionary is methods exposed by the IDictionary interface you can pass the concreate class round as an interface - which reduces the coupling between methods.


I think everyone has already pointed out that IDictionary(Of String, String) is an interface that cannot be instantiated as an object and that Dictionary(Of String, String) is a class that can be instantiated as an object. However, it's important to know why you might use one versus another.

Let's say you are going to define a method to search a dictionary and return the value associated with the key; if you don't find a value, you want to return an emtpy string. You need a dictionary, so you decide to go ahead and use .NET's Dictionary class for your implementation:

Function GetValueFromKey(ByVal dict As Dictionary(Of String, String),
                         ByVal key As String) As String
    If ''//you find the value in the dictionary
         return theValue
    Else ''//you can't find the value in the ditionary
         return String.Empty
    End If
End Function

You implement the code and your method is working great. However, after a few months, you notice that your program is starting to slow down. As you dig deep into the problem, you realize that .NET's implementation of Dictionary isn't ideal when your key data is 50,000 characters in length (NOTE: I honestly don't know if there are any real issues with .NET's Dictionary implementation; this is just an example), and all of your key strings are over 50,000 characters in length.

So now you have to figure out a new way of looking up the data without using .NET's Dictionary class. You're going to have to use a new data type and then everywhere in your code that you have GetValueFromKey(...), you're going to have to make modifications to use the new datatype. This isn't the end of the world, but maybe there's a better way.

When you first coded your GetValueFromKey method, you might have thought "I need to retrieve a value from a dictionary. It doesn't matter what kind of dictionary it is, but I need to get the value out of it." In that case, you could choose to use the IDictionary interface like so:

Function GetValueFromKey(ByVal dict As IDictionary(Of String, String),
                         ByVal key As String) As String
    If ''//you find the value in the dictionary
         return theValue
    Else ''//you can't find the value in the ditionary
         return String.Empty
    End If
End Function

Everything about that function looks the same except that Dictionary was changed to IDictionary? Did your implementation of the function change? No. IDictionary is simply a contract that defines the properties and methods a dictionary needs to have. All your function cares about is calling the properties and methods of dictionaries. Your function doesn't care how it is implemented.

In this way, if you had programmed to an IDictionary interface instead of to a Dictionary implementation, when your program slowed down and you were forced to change your implementation of your dictionary, you could have simply written your own dictionary class that implements IDictionary. As long as your new class follows the contract specified in IDictionary, none of your other code needs to change in order to deal with the new dictionary class that you created.

The design principle "Program to an interface and not to an implementation" helps you to deal with change. If you start to learn how to use interfaces in your code, you'll be better prepared to deal with change.


IDictionary is just an interface - you can't specificly create an instance of it. Dictionary is one implementation of IDictionary, and by that definition Dictionary can do everything that IDictionary can, maybe more.

If you wanted to create your own Dictionary class that worked differently from Dictionary, you would implement IDictionary.


Well, Dictionary<TKey, TValue> is a class that implements interface IDictionary<TKey, TValue>.

That would bring you to a ton of differences, but basically, you can't instantiate interface, while you can instantiate a class :)


Dictionary(Of String, String) is a concrete implementation of the IDictionary(Of String, String) interface.

If you declare a member as type Dictionary(Of String, String) it can only be assigned instances of Dictionary(Of String, String), or sub-classes of it. But if you define the member as IDictionary(Of String, String) it can be assigned an instance of any type that implements IDictionary(Of String, String), including Dictionary(Of String, String).

Declaring members using interfaces instead of types when possible allows greater flexibility.


First of all I want to remember that IDictionary<TKey, TValue> is an interface where as Dictionary is a concrete class which implements the IDictionary and it's done inside library.

As far as I have seen there is some differences according to the library between IDictionary and `Dictionary

What is the difference between dictionary(Of String, String) and IDictionary(Of String, String)

Look on only a few functions and variables are are defined in IDictionary interface.

What is the difference between dictionary(Of String, String) and IDictionary(Of String, String)

Whereas Dictionary literally providing more functions and variables which have all of the functions and variables of the IDictionary. So in my opinion Dictionary providing more flexibility than IDictionary

However, according to this documentation IDictionary is more time consuming than Dictionary as the author have shown some real time execution of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜