Collections - Items to Remember
I am new to C#.Working in ASP.NET 3.5 ( C# 3.0). What are 开发者_Go百科the collection classes do i need to familiar with inorder to develop effective code ? like IList, and counterpart IList<T>
,List
,List<T>
are enough ?
Thank you very much to all ,for giving me a very good guidance.
Apart from List<T>
and Dictionary<TKey, TValue>
I would also urge you to get familiar with the IEnumerable<T>
interface (and the extension methods around it) and, above all, the habit of exposing IEnumerable<T>
as a first-hand choice until it is proven that you need to return a "richer" interface from methods.
Most of the time I stick with List<>
and Dictionary<>
but with the introduction of LINQ to Objects I find that it's pretty rare that I need to instantiate these classes anymore. I'm also passing my lists around as IEnumerable<>
much more too.
For a good summary of the available collections, see Commonly Used Collection Types.
Each collection has it strengths and purposes. I find I use List<T>
and Dictionary<TKey, TValue>
most frequently. You can be reasonably effective just knowing these two.
However to be most effective you should be familiar with at least the purpose of all of them so that you can choose the right one. For example, does your collection represent a queue of things to do? Or is it Stack? Does the value of an Item also act as its identity, you have a lot of them and you need to retrieve them quickly, then Hashset may be what you need.
Which ones you need to be familiar with depend on what you need to do.
If you want to find out about collection classes then check out the System Collections page on MSDN.
In my mind it is good to be aware of anything that implements ICollection, IDictionary, IEnumerable, and IList.
Dictionary<K,V>
and OrderedDictionary<K,V>
are good to know for storing strongly typed key value pairs.
The new set class HashSet<T>
which implements ICollection<T>
, and returns false for Add(...)
if you're adding a pre-existing value, is also a good one for the tool-box
精彩评论