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
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;
}
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:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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
Member
104 Points
265 Posts
Idictionary vs Dictionary return in method.
Nov 11, 2017 05:26 AM|Khan_1|LINK
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
Participant
1627 Points
457 Posts
Re: Idictionary vs Dictionary return in method.
Nov 14, 2017 06:19 AM|Rohit Rao|LINK
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:
Let me know if you need more details.
Thanks
Rohit
Star
8670 Points
2882 Posts
Re: Idictionary vs Dictionary return in method.
Nov 14, 2017 08:16 AM|Cathy Zou|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
14 Posts
Re: Idictionary vs Dictionary return in method.
Mar 05, 2020 06:37 AM|EvanChatter|LINK
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