locked
Idictionary vs Dictionary return in method. RRS feed

  • Question

  • User-240513752 posted

    I made a method for fetching data as Dicitonary, I thought that dic vs Idic has not much difference, but when I compiled the project it was building successfully, because I am using Dictionary but in returning from method ,I was returning Idictionary.
    as i change the Idictionary to dictionary then it built successfully.

    I am confused little bit , because both has same declaration.

    b) I need some practice material to get value of dictionary which has class. the best way

    Saturday, November 11, 2017 5:26 AM

All replies

  • User-1629691846 posted

    IDictionary is an interface. We can think it as a Contract that provides information (Properties / Methods) of what an implementing class will do.

    Dictionary is a Class or we can say the Concrete class that implements IDcitionary interface, So you will get all the method (Contract) of IDictionary implemented in Dictionary class. The following code will always work as Dictionary class Implements IDictionary interface:

     static void Main(string[] args)
            {
                IDictionary<int, string> Result = TestMethod();
    
                foreach (string name in Result.Values)
                {
                    Console.WriteLine(name);
                }
                Console.ReadLine();
            }
    
    
    
            public static IDictionary<int, string> TestMethod()
            {
                Dictionary<int, string> Name = new Dictionary<int, string>();
                Name.Add(1, "sourav");
                Name.Add(2, "Ram");
                Name.Add(3, "shyam");
    
                return Name;
            }

    Let me know if you need more details.

    Thanks

    Rohit

    Tuesday, November 14, 2017 6:19 AM
  • User-707554951 posted

    Hi Khan_1,

    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.

    For more description about this, you could refer to following links:

    https://stackoverflow.com/a/1595519

    https://msdn.microsoft.com/en-us/library/system.collections.idictionary(v=vs.110).aspx

    http://www.tutorialsteacher.com/csharp/csharp-dictionary

    Best regards

    Cathy

    Tuesday, November 14, 2017 8:16 AM
  • User521825913 posted

    The difference is that Dictionary is concrete implementation while IDictionary is just a contract, abstraction. If IDictionary is a "more generic" type than Dictionary then it makes sense to use the more generic type in declaring variables. It is a good idea to abstract yourself from an implementation, by that I mean programming against an interface rather than a concrete implementation

    Thursday, March 5, 2020 6:37 AM