At its simplest, an object holds a single value.
At its most complex, it holds references to many other objects.
The .NET Framework provides collections—these include List and Dictionary.
They are often useful.
List
Dictionary
ArrayList
Hashtable
LinkedList SortedList
Data types comprising collections of abstract objects are a central
object of study in computer science.
Sedgewick, p. 143
List
First, the List type provides an efficient and dynamically-allocated array. It does not provide fast lookup in the general case—the Dictionary is better for this. List is excellent when used in loops.List
Program that uses List type [C#] using System; using System.Collections.Generic; class Program { static void Main() { // Use the List type. List<string> list = new List<string>(); list.Add("cat"); list.Add("dog"); foreach (string element in list) { Console.WriteLine(element); } } } Output cat dog
Dictionary
The Dictionary type in the base class library is an important one. It is an implementation of a hashtable, which is an extremely efficient way to store keys for lookup. The Dictionary in .NET is well-designed.Dictionary
We try to reference items in a table directly by doing arithmetic
operations to transform keys into table addresses.
Sedgewick, p. 587
Program that uses Dictionary [C#] using System; using System.Collections.Generic; class Program { static void Main() { // Use the dictionary. Dictionary<string, int> dict = new Dictionary<string, int>(); dict.Add("cat", 1); dict.Add("dog", 4); Console.WriteLine(dict["cat"]); Console.WriteLine(dict["dog"]); } } Output 1 4
Note:
This program shows how the Dictionary can be used with type
parameters to store keys and values of specific types.
Tip:
Many of the fastest and most powerful collections are found in
System.Collections.Generic.
These are generic types.
Generic ClassArrayList
Next, the ArrayList is a collection found in System.Collections and it can store objects of any derived type. You don't need to worry about the type of the elements, at least until you need to use them.ArrayList
Program that uses System.Collections [C#] using System; using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList(); list.Add("cat"); list.Add(2); list.Add(false); foreach (var element in list) { Console.WriteLine(element); } } } Output cat 2 False
Hashtable
To continue, the Hashtable is a lookup data structure that uses a hash code to find elements quickly. The newer Dictionary collection is usually more appropriate for programs when available.Hashtable
Program that uses Hashtable [C#] using System; using System.Collections; class Program { static void Main() { Hashtable table = new Hashtable(); table["one"] = 1; table["two"] = 2; // ... Print value at key. Console.WriteLine(table["one"]); } } Output 1
Lists
Lists are linear—one element is stored after the other. The List generic type is often the best implementation available for its purpose on the .NET Framework. There are other versions of lists.LinkedList SortedList
Also:
A linear collection, such as an array or List,
can be wrapped and made read-only with ReadOnlyCollection.
ReadOnlyCollection
Performance:
Many collections (including List and Dictionary) can be optimized by using a capacity.
This allocates extra initial memory.
Capacity
0 comments:
Post a Comment