Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | In this lecture we review the collection classes of the C# library. We concentrate our efforts on the generic, type parameterized collection classes. The main parts of the lecture are about the list classes and the dictionary classes. At the end of the lecture we compare with the older, non-generic collection classes. |
Collections - History and Overview |
A historic View on Collection Programming Slide Annotated slide Contents Index References Textbook |
|
|
|
Generic Collections in C# |
Overview of Generic Collections in C# Slide Annotated slide Contents Index References Textbook |
|
|
The class and interface inheritance tree related to Lists |
The Interface IEnumerable<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
|
|
The Interface ICollection<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
The Interface IList<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
Overview of the class Collection<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
Sample use of class Collection<T> Slide Annotated slide Contents Index References Textbook |
|
Program: Basic operations on a Collection of characters. A C# 3.0 Program. |
|
Program: Output of the program with basic operations on a Collection of characters. |
|
|
Specialization of Collections Slide Annotated slide Contents Index References Textbook |
|
Program: A class Animal. |
|
Program: A class AnimalFarm - a subclass of Collection<Animal> - testing protected members. |
|
Program: A sample client of AnimalFarm - revealing use of protected Collection<Animal> methods. |
|
Program: Output from sample client of AnimalFarm. |
|
|
Specialization of Collections - a realistic example Slide Annotated slide Contents Index References Textbook |
|
Program: The class Animal - Unchanged. |
|
Program: The class AnimalFarm - a subclass of Collection<Animal>. |
|
Program: A sample client of AnimalFarm. |
|
Program: Output from sample client of AnimalFarm. |
|
Overview of the class List<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
Sample use of class List<T> Slide Annotated slide Contents Index References Textbook |
|
|
Program: Basic operations on a List of characters. A C# 3.0 program. |
|
Program: Output of the program with basic operations on a List of characters. |
|
|
Sample use of the Find operations in List<T> Slide Annotated slide Contents Index References Textbook |
|
Program: Struct Point - used as actual List type parameter. |
|
Program: Sample uses of List.Find. |
|
Program: Output from the Find program. |
|
|
Sample use of Sort in List<T> Slide Annotated slide Contents Index References Textbook |
|
Program: Four different activations of the List.Sort method. |
|
Program: Output of the sorting program. |
|
|
Exercise 12.3. Shuffle List | Write a Shuffle operation that disorders the elements of a collection in a random fashion. A shuffle operation is useful in many context. There is no Shuffle operation in System.Collections.Generic.List<T>. In the similar Java libraries there is a shuffle method. In which class do you want to place the Shuffle operation? You may consider to make use of extension methods. You can decide on programming either a mutating or a non-mutating variant of the operation. Be sure to understand the difference between these two options. Test the operation on List<Card>. The class Card (representing a playing card) is one of the classes we have seen earlier in the course. |
Exercise 12.3. Course and Project classes | In the earlier exercise about courses and projects (found in the lecture about abstract classes and interfaces) we refined the program about BooleanCourse, GradedCourse, and Project to use a common superclass called Course. Revise your solution (or the model solution) such that the courses in the class Project are represented as a variable of type List<Course> instead of by use of four variables of type Course. Reimplement and simplify the method Passed in class Project. Take advantage of the new representation of the courses in a project, such that the "3 out of 4 rule" (see the original exercise) is implemented in a more natural way. |
Sample use of BinarySearch in List<T> Slide Annotated slide Contents Index References Textbook |
|
Program: Sample uses of List.BinarySearch. |
|
Program: Output from the BinarySearch program. |
|
Program: Searching for a non-existing Point. |
|
Program: Output from the BinarySearch program - non-existing Point. |
|
|
Overview of the class LinkedList<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
The class LinkedListNode<T> Slide Annotated slide Contents Index References Textbook |
|
|
|
Sample use of class LinkedList<T> Slide Annotated slide Contents Index References Textbook |
Program: Basic operations on a LinkedList of integers. |
|
Program: Output of the program with basic operations on a LinkedList. |
|
Program: Basic operations on a LinkedList of integers - using LinkedListNodes. |
|
Program: Output of the program with LinkedListNode operations on a LinkedList. |
|
Time complexity overview: Collection classes Slide Annotated slide Contents Index References Textbook |
|
Table. Time complexities of important operations in the classes Collection<T>, List<T>, and LinkedList<T>. |
|
|
Using Collections through Interfaces Slide Annotated slide Contents Index References Textbook |
|
Program: An animal program - firmly bound to Collections - a problematic choice. |
|
Program: A program based on ICollection<Animal> - with a Collection<Animal>. |
|
Program: A program based on ICollection<Animal> - with a List<Animal>. |
|
Program: Output from animal collection programs. |
|
Generic Dictionaries in C# |
Dictionaries Slide Annotated slide Contents Index References Textbook |
|
|
|
Overview of Generic Dictionaries in C# Slide Annotated slide Contents Index References Textbook |
|
|
The class and interface inheritance tree related to Dictionaries |
|
The interface IDictionary<K,V> Slide Annotated slide Contents Index References Textbook |
|
|
|
Overview of the class Dictionary<K,V> Slide Annotated slide Contents Index References Textbook |
|
|
Sample use of class Dictionary<K,V> Slide Annotated slide Contents Index References Textbook |
|
Program: A class Person. |
|
Program: A class BankAccount. |
|
Program: A program working with Dictionary<Person,BankAccount>. |
|
Program: Output from the Dictionary<Person,BankAccount> program. |
|
Exercise 12.4. Switching from Dictionary to SortedDictionary | The program on this slide instantiates a Dictionary<Person,BankAccount>. As recommended earlier in this lecture, we should work with the dictionary via a variable of the interface type IDictionary<K,V>. You are now asked to replace Dictionary<Person,BankAccount> with SortedDictionary<Person,BankAccount> in the above mentioned program. This causes a minor problem. Identify the problem, and fix it. Can you tell the difference between the output of the program on this slide and the output of your revised program? You can access the BankAccount and Person classes in the web version of the material. |
Notes about Dictionary Classes Slide Annotated slide Contents Index References Textbook |
|
|
Time complexity overview: Dictionary classes Slide Annotated slide Contents Index References Textbook |
|
Table. Time complexities of important operations in the classes Dictionary<K,V>, SortedDictionary<K,V>, and SortedList<K,V>. |
|
|
Non-generic Collections in C# |
The non-generic collection library in C# Slide Annotated slide Contents Index References Textbook |
|
The class and interface inheritance tree related to collections |
|
Notes about non-generic Collections Slide Annotated slide Contents Index References Textbook |
|
Patterns and Techniques |
The Iterator Design Pattern Slide Annotated slide Contents Index References Textbook |
|
|
|
|
Exercise 12.6. Explicit use of iterator - instead of using foreach | In this program we will make direct use of an iterator (an enumerator) instead of traversing with use of foreach. In the animal collection program, which we have seen earlier in this lecture, we traverse the animal collections several times with use of foreach. Replace each use of foreach with an application of an iterator. |
Exercise 12.6. Using multiple interators | In this exercise we will see how to make good use of two interators of a single collection. Our starting point will be the type Interval, and the iterator which we have programmed for this type earlier in this teaching material. For a given interval I, generate a list of all possible pairs (e,f) where both e and f come from I. As an example, the interval new Interval(1,2) should give rise to the list (1, 1), (1, 2), (2, 1), and (2, 2). For the purpose of generating the list of all such pairs, request two iterators from the interval, and traverse the interval appropriately in two nested while loops. Like in the previous exercise, it is suggested that you use the operations in IEnumerator. Such a solution gives the best understanding of the use of multiple iterators. It is, however, also possible to solve this exercise by nested foreach loops. |
Making iterators with yield return Slide Annotated slide Contents Index References Textbook |
|
Program: A collection of up to three instance variables of type T - with an iterator. |
|
Program: A sample iteration of the three instance variable collection. |
|
Exercise 12.7. The iterator behind a yield | Reprogram the iterator in class GivenCollection without using the yield return statement in the GetEnumerator method. |
|
Program: The abstract class IntSequence - Revisited. |
|
Program: The class IntInterval - Revisited. |
|
Program: The class IntSingular - Revisited. |
|
Program: The class IntCompSeq - Revisited. |
|
Program: An application of the integer sequences. |
|
Program: Output of the integer sequence application program. |
|
|
Program: Animalfarm - GetGroup with yield return. |
|
Program: A sample client of AnimalFarm. |
|
Program: Program output. |
|
Iterator blocks and yield return Slide Annotated slide Contents Index References |
|
Program: A method formed by an iterator block. |
|
|
|
Exercise 12.8. Infinite Collections of Integers | In several respects, this exercise previews ideas from LINQ. Program a class IntegersFrom which represent an inifinite sequence of integers (of type long) from a given starting point. The class must implement the interface IEnumerable<long>. First, program a version without use of yield return, and next program a version which uses yield return. Compare the two versions. (It is surprisingly tricky to program the version which uses native iterators (enumerators) in C#, without using yield return. You may chose to study my implementation (in the solution) instead of spending time on programming the class yourself.) As an alternative to the class IntegersFrom, make an extension method AdInifinitum (which means 'to inifinity') in the type long , which enumerates an infinite sequence of integers from a given starting point: public static IEnumerable Make an additional extension method in the interface IEnumerable<long> which filters an infinite sequence of integers with use of a given predicate public static IEnumerable<long> Filter(this IEnumerable<long> source, Func<long, bool> pred){...} Use Filter to obtain all even integers, and all primes. Finally, program an extension method in IEnumerable<long> that adds two infinite sequences of integers together (pair-by-pair): public static IEnumerable<long> Add(this IEnumerable<long> source, IEnumerable<long> other){...} Add all natural numbers, lL.AdInfinitum(), to itself, and get all even numbers again. |
Chapter 12: Collection Classes
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:21:16