List
stack.Push(100);
stack.Push(1000);
stack.Push(10000);
return stack;
[/sourcecode]
First, the List type provides you with an efficient and dynamically-allocated array. It does not provide fast lookup in the general case, which you will want to use Dictionary for. It is excellent when used in loops.
[sourcecode language="csharp"]
// Use the List type.
List<string> list = new List<string>();
list.Add("cat");
list.Add("dog");
List<string> list = new List<string>();
list.Add("cat");
list.Add("dog");
foreach (string element in list)
{
Console.WriteLine(element);
}
[/sourcecode]
{
Console.WriteLine(element);
}
[/sourcecode]
Dictionary
The Dictionary type in the base class library is one of the most important ones you need to use for your C# programs. 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.
[sourcecode language="csharp"]The Dictionary type in the base class library is one of the most important ones you need to use for your C# programs. 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.
// Use the dictionary.
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("cat", 1);
dict.Add("dog", 4);
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("cat", 1);
dict.Add("dog", 4);
Console.WriteLine(dict["cat"]);
Console.WriteLine(dict["dog"]);
[/sourcecode]Console.WriteLine(dict["dog"]);
ArrayList
As shown in this program, 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 know their types to use them.
[sourcecode language="csharp"]As shown in this program, 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 know their types to use them.
ArrayList list = new ArrayList();
list.Add("cat");
list.Add(2);
list.Add(false);
list.Add("cat");
list.Add(2);
list.Add(false);
foreach (var element in list)
{
Console.WriteLine(element);
}
[/sourcecode]{
Console.WriteLine(element);
}
Hashtable
Hashtable optimizes lookups. This type is used in the C# language. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type. First, you can create a new Hashtable with the simplest constructor. When it is created, the Hashtable has no values. We directly assign values with the indexer, which uses the square brackets [ ]. The example adds three integer keys with one string value each.
[sourcecode language="csharp"]Hashtable optimizes lookups. This type is used in the C# language. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type. First, you can create a new Hashtable with the simplest constructor. When it is created, the Hashtable has no values. We directly assign values with the indexer, which uses the square brackets [ ]. The example adds three integer keys with one string value each.
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
[/sourcecode]{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
Stack
Stack is a LIFO collection. It provides a powerful and simple last-in-first-out data structure. This can help you develop parsers quickly and also replace complex recursive algorithms. Stack is a generic type.
Stack is a LIFO collection. It provides a powerful and simple last-in-first-out data structure. This can help you develop parsers quickly and also replace complex recursive algorithms. Stack is a generic type.
LIFO:
The last element added (with Push) to Stack is the first one removed (with Pop).
[sourcecode language="csharp"] Stack<int> stack = new Stack<int>();The last element added (with Push) to Stack is the first one removed (with Pop).
stack.Push(100);
stack.Push(1000);
stack.Push(10000);
return stack;
Queues
Queue is a FIFO collection. It helps when you have elements that you need to process in a first-in, first-out order. It processes the elements that you received longest ago first. We look at several examples of using Queue<T>. We think about some of the logic used for processing help requests in a C# application.
Queue is a FIFO collection. It helps when you have elements that you need to process in a first-in, first-out order. It processes the elements that you received longest ago first. We look at several examples of using Queue<T>. We think about some of the logic used for processing help requests in a C# application.
First-In-First-Out:
The queue data structure implements this algorithm. Queue is a generic type with one type parameter.
[sourcecode language="csharp"]The queue data structure implements this algorithm. Queue is a generic type with one type parameter.
// New Queue of integers
Queue<int> q = new Queue<int>();
Queue<int> q = new Queue<int>();
q.Enqueue(5); // Add 5 to the end of the Queue.
q.Enqueue(10); // Then add 10. 5 is at the start.
q.Enqueue(15); // Then add 15.
q.Enqueue(20); // Then add 20.
[/sourcecode]q.Enqueue(10); // Then add 10. 5 is at the start.
q.Enqueue(15); // Then add 15.
q.Enqueue(20); // Then add 20.