0

If you go to this link in stackoverflow. There is a question about converting an Ienrumerable to Dictionary. So my question is why would we need IEnumerable when we have the dictionary object?

Actually, i am trying to figure out what is the difference between them.

Community
  • 1
  • 1
user2688063
  • 293
  • 2
  • 4
  • 15

3 Answers3

5

IEnumerable is an interface. An interface can not be instantiated by itself, but have to be instantiated as a class that implements the given interface.

Dictionary is a hash-map datastructure that implements the IEnumerable interface, hence it have all the methods and properties that the IEnumerable interface requires its implementations to have, making it possible to Enumerate the Dictionary structure.

In short: IEnumerable is an interface and Dictionary is a class implementing the interface.

Jite
  • 5,761
  • 2
  • 23
  • 37
  • Yes so that is what I mean you use interface to create classes and and IEnumerable is an interface not a class. And that interface is implemented by a Dictionary. So since Dictionary is uses IEnumerable why wouldnt someone just return Dictionary object. Anyway, i must be missing something I am into my studies now so I can start to get some sense. – user2688063 Sep 13 '13 at 04:51
  • The IEnumerable inheritage pretty much only promise that the object is possible to iterate. A Dictionary is always IEnumberable, a IEnumerable object is not always a Dictionary. – Jite Sep 13 '13 at 05:17
  • Thnaks Jite for explanation, but what I am saying. IEnumerable is an interface. And if i read the definition of interface it says interfaces are implemented in other classes and their members must be defined in called class. If i follow the definition then I am so confused by which principle I can directly assign values to an interface. Anyway it is my last post in this topic thanks. – user2688063 Sep 17 '13 at 02:48
  • 1
    @Jite this was a great edit. I am glad to see someone actually answered the question asked instead of insulting the asker, like many others who left comments/answers on this question – Keith Jan 19 '19 at 00:13
2

well, the question you've linked is on the opposite question. how to convert from IEnumerable to a Dictionary. why will we convert the other way around? we will probably won't. but you can also ask why will someone convert a specific class to object?

the answer is the sometimes you want to have a variable/method parameter/ class member that can be one of many things: a dictionary or something else. for that you might use IEnumerable.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
1

So my question is why would we need IEnumerable when we have the dictionary object?

The question you've referred is about opposite: creating Dictionary from IEnumerable. So you have IEnumerable and you'd like to have Dictionary.

It can be required when you have e.g. e List and you'd like to have quicker access to collection elements by one of the items property value.

And just to be sure, Dictionary<TKey, TValue> already implements IEnumerable<T>, so the conversion would not necessary in the opposite direction (unless you're trying to change the T).

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Sorry guys I am trying to learn. I was reading about Generics objects and it says Dictionary and all Generics objects uses IEnumerable interface. I am not really clear with the answers due to lack of my knowledge but i will check back after some more readings. – user2688063 Sep 11 '13 at 19:05