Collection Classes in C# • There are five types of collection classes – non-generic, we will not cover these since they exist for backward compatibility – generic, these are very similar to Java, we will introduce through a table of operations – bit-based, a quick look with focus on operations – specialized, just to let you know they are here – concurrent, will cover later in the course • We also look at iterators and enumerators • This material was borrowed from C# 4.0: The Complete Reference by Herbert Schildt
33
Embed
Collection Classes in C#blk/cs3490/ch09/Collections.pdfCollection Classes in C# ... zero if x is greater than y, zero if the two objects are the same, and less than zero if x is less
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Collection Classes in C#• There are five types of collection classes
– non-generic, we will not cover these since they exist for backward compatibility
– generic, these are very similar to Java, we will introduce through a table of operations
– bit-based, a quick look with focus on operations
– specialized, just to let you know they are here
– concurrent, will cover later in the course
• We also look at iterators and enumerators
• This material was borrowed from C# 4.0: The Complete Reference by Herbert Schildt
The Generic Collections
• Note: in C# style programming it is customary to put I before the names of interfaces
ICollection<T> Methods
IList<T> Methods
IDictionary<TKey,
TValue> Methods
andProperties
ISet <T>
IEnumerable<T> and IEnumerator<T>• IEnumerable<T>
– IEnumerable<T> declares the GetEnumerator( ) method
– It returns an enumerator of type T for the collection
• IEnumerator<T> has two methods
– MoveNext( ) and Reset( )
– It also declares T Current { get; } that returns a T reference to the next object
– Notice how C# allows getter and setter methods to be added by using { get; set; } as needed
IComparer<T> and IEqualityComparer<T>
• IComparer<T>
– Must implement the method int Compare(T x, T y)
– This method compares x with y and returns greater than zero if x is greater than y, zero if the two objects are the same, and less than zero if x is less that y.
• IEqualityComparer<T>
– Two methods: bool Equals(T x, T y) andint GetHashCode(T obj)
– Equals( ) must return true if x and y are equal.
– GetHashCode( ) must return the hash code for obj.
– If two objects compare as equal, then their hash codes must also be the same.
The Core Generic Classes Themselves
Selected Methods for List - 1
Selected Methods for List - 2
SelectedMethods
forLinked List
Methods and Properties for Dictionary
Methods and Properties for SortedDictionary
Methods and Properties for SortedList
Stack and Queue Methods
Homework Exercises• Consider your project and try to propose a use for
– A generic List or SortedList
– A generic Dictionary or SortedDictionary
to hold data you will be processing
• If you cannot come up with two examples of these structures can you use a set or a stack or a queue?
• Show the declaration of your choices specifying the types to be used
• Describe how the collections will be used in the overall project application
Methods in BitArray
• BitArrays can be indexed. Each index specifies an individual bit, with an index of zero indicating the low-order bit.
The Specialized Collections
foreach loops in C#• A simple examplepublic IEnumerator GetEnumerator() {
foreach(char ch in chrs)
yield return ch;
}
• The yield return statement returns the next object in the collection
Using IComparable<T>• You need to implement the CompareTo method
int CompareTo(T other)
– To sort in ascending order, return zero if the objects are equal, a positive value if the invoking object is greater than other, and a negative value if the invoking object is less than other.
– To sort in descending order, reverse the outcome of the comparison.
• Example let public Inventory(string n, double c, int h) { name = n; cost = c; onhand = h; }
public int CompareTo(Inventory other) { return string.Compare(name, other.name, StringComparison.Ordinal);
} // compares based on string name
Using a Generic List - 1class Inventory {
string name;
double cost;
int onhand;
public Inventory(string n, double c, int h) {
name = n;
cost = c;
onhand = h;
}
public override string ToString() {
return String.Format("{0, -10} Cost: {1, 6: C} On hand: {2}",
name, cost, onhand);
}
}
class TypeSafeInventoryList {
static void Main() {
List<Inventory> inv = new List<Inventory>();
// Add elements to the list
inv.Add(new Inventory( "Pliers", 5.95, 3) );
Using a Generic List - 2inv.Add(new Inventory( "Wrenches" , 8.29, 2) );