Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between DictionaryBase and Dictionary<Tkey,Tvalue>

Can anyone please tell me the difference between DictionaryBase and generic Dictionary<TKey, TValue>, except that DictionaryBase is an abstract class! Both of them are strongly typed and I read somewhere that generic classes are better than non-generic ones in performance and should be preferred, so what is the exact use of DictionaryBase then? Both the classes are almost same!

Any kind of help would be appreciated

Thanks

like image 879
Batrickparry Avatar asked Jan 06 '11 11:01

Batrickparry


2 Answers

The reason the non-generic collection types are still in the .NET framework is largely for backwards compatibility. Generally you should use the generic collection types whenever possible.

like image 121
Adam Ralph Avatar answered Oct 04 '22 09:10

Adam Ralph


Dictionarybase is base class implementing the non generic IDictionary interface while Dictionary is the generic version of a dictionary implementing the generic IDictionary interface. Use of generics is preferred over non generics.

I think this would be more helpful When to use generics?

Using generic collections is generally recommended, because you gain the immediate benefit of type safety without having to derive from a base collection type and implement type-specific members. Generic collection types also generally perform better than the corresponding nongeneric collection types (and better than types that are derived from nongeneric base collection types) when the collection elements are value types, because with generics there is no need to box the elements.

Also go through, Introduction to Generics

Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot. Generics are most frequently used with collections and the methods that operate on them. Version 2.0 of the .NET Framework class library provides a new namespace, System.Collections.Generic, which contains several new generic-based collection classes. It is recommended that all applications that target the .NET Framework 2.0 and later use the new generic collection classes instead of the older non-generic counterparts

like image 43
Devendra D. Chavan Avatar answered Oct 04 '22 09:10

Devendra D. Chavan