0% found this document useful (0 votes)
69 views

Generic Collections: Microsoft Certification Exam 70-486

The document discusses generic collection classes in C#, specifically List<T>, Queue<T>, and Dictionary<K,V>. It provides examples of adding, removing, and accessing elements in a typed List and Queue. It also demonstrates storing key-value pairs in a Dictionary and accessing elements via keys. The document explains that generics allow type-safe collections as opposed to untyped object collections, and that certain collection classes like List<T> have methods for sorting and indexing elements.

Uploaded by

Carlos Lopera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Generic Collections: Microsoft Certification Exam 70-486

The document discusses generic collection classes in C#, specifically List<T>, Queue<T>, and Dictionary<K,V>. It provides examples of adding, removing, and accessing elements in a typed List and Queue. It also demonstrates storing key-value pairs in a Dictionary and accessing elements via keys. The document explains that generics allow type-safe collections as opposed to untyped object collections, and that certain collection classes like List<T> have methods for sorting and indexing elements.

Uploaded by

Carlos Lopera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

C# Generic Collections

Microsoft Certification Exam 70-486

Generalized Collection Classes

Array-like data structures (Chapter 10)

ArrayList
Queue
Stack
Hashtable
SortedList

Offer programming convenience for specific


access needs.
Store objects

Add anything.
Typecast on removal.
2

Generics

Generics (Chapter 18) offer typesafe


alternatives.

New in .NET 2.0 (Visual Studio 2005)

Generics are generally the preferred


alternative.

Object collection classes offer advantages in


certain unusual cases.

Collection Classes

To use these classes we must write

using System.Collections.Generic;

for generics
Included in the default C# template

using System.Collections;

for object collections


Not included in the default C# template

Generics

C# generics are like C++ templates.

Classes written with blanks to be filled


in by the user (parameters)

Cannot be instantiated directly.


We create a specialized version for a
specific type by supplying the name of the
type when the class is used.

Not a macro!

Supplying the parameter effectively creates a


new class, which can be instantiated.
5

The Generic List Class

List<T> is the generic List class

Provides traditional list operations

T represents a class name parameter to be


supplied in declarations.

Insert
Delete

Also provides array operations

Access by position, using index notation


6

List<T> Methods

Add (T item)

Insert (int index, T item)

Insert item at a specific position

Remove (T item)

Add item at end of list

Remove first occurance of item

RemoveAt (int index)

Remove item at specified position


7

List<T> Methods

Clear()

bool Contains(T item)

Determines if item is in the list

int IndexOf(T item)

Removes all items from the list

Returns index of item, or -1 if not in list.

Sort()

... more
8

List<T> Indexer

Array index notation can be used to get


or set a specified item.

int_list[5] = 17;

int temp = int_list[5];

Throws an exception if int_list[5] does not exist.

List<T> Properties

Count

Number of items in the list

10

List<T> Example

Create new C# Console Application


project.

11

List<int> Example
static void Main(string[] args)
{
List<int> ints = new List<int>();
ints.Add(1);
ints.Add(2);
ints.Add(3);
foreach (int i in ints)
{
Console.WriteLine(i.ToString() );
}
Console.ReadLine();
}

12

List<int> Example Running

13

List<Circle> Example
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<Circle> circles = new List<Circle>();
circles.Add(new Circle("C1", 1.0));
circles.Add(new Circle("C2", 2.0));
circles.Add(new Circle("c3", 3.0));
foreach (Circle c in circles)
{
Console.WriteLine(c.Name());
}
Console.ReadLine();
}
}

14

List<Circle> Example Running

15

Accessing List by Position


static void Main(string[] args)
{
List<Circle> circles = new List<Circle>();
circles.Add(new Circle("C1", 1.0));
circles.Add(new Circle("C2", 2.0));
circles.Add(new Circle("c3", 3.0));
for (int i = 0; i < circles.Count; ++i)
{
Console.WriteLine(circles[i].Name());
}
Console.ReadLine();

Same result
16

Inserting
static void Main(string[] args)
{
List<Circle> circles = new List<Circle>();
circles.Add(new Circle("C1", 1.0));
circles.Add(new Circle("C2", 2.0));
circles.Add(new Circle("c3", 3.0));
Circle c4 = new Circle("C4", 4.0);
circles.Insert(2, c4);
for (int i = 0; i < circles.Count; ++i)
{
Console.WriteLine(circles[i].Name());
}
Console.ReadLine();
}
17

Inserting

18

Inserting and Deleting


static void Main(string[] args)
{
List<Circle> circles = new List<Circle>();
circles.Add(new Circle("C1", 1.0));
circles.Add(new Circle("C2", 2.0));
circles.Add(new Circle("c3", 3.0));
Circle c4 = new Circle("C4", 4.0);
circles.Insert(2, c4);
circles.RemoveAt(1);
for (int i = 0; i < circles.Count; ++i)
{
Console.WriteLine(circles[i].Name());
}
Console.ReadLine();
}

19

Inserting and Deleting

20

Sorting

List<T> has a Sort method.


Parameter class must implement the
IComparable<T> interface.
Example:

class Schedule_Record : IComparable<Schedule_Record>


{
private String college;
...

IComparable interface

Class must implement CompareTo()


method, taking an object of the same
type as its parameter.

Return negative int if this object < other


Return 0 if this object == other
return positive int if this object > other

Same as strcmp() in C

Implementing IComparable
class Circle : IComparable<Circle>
{
private String name;
private double radius = 0.0;
...
public int CompareTo(Circle other)
{
if (this.radius < other.radius)
return -1;
else if (this.radius > other.radius)
return 1;
else
return 0;
}

Using Sort()

Program Running

End of Section

The Generic Queue

Queue<T>
http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

Methods

Enqueue (T item)

T Dequeue()

Add an item to the end of the queue

Removes and returns object at the head of the


queue

Clear(), Contains(), Peek(), ... many more


26

Queue<Circle> Example
static void Main(string[] args)
{
Queue<Circle> circles = new Queue<Circle>();
circles.Enqueue(new Circle("C1", 1.0));
circles.Enqueue(new Circle("C2", 2.0));
circles.Enqueue(new Circle("c3", 3.0));
while (circles.Count > 0)
{
Circle c = circles.Dequeue();
Console.WriteLine(c.Name());
}
Console.ReadLine();
}

27

Queue<Circle> Example Running

28

Dictionary<K,V>

http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx

Stores (Key, Value) pairs

Class KeyValuePair<K,V>

Template parameters

K is type of Key
V is type of Value

29

Dictionary<K,V> Example
static void Main(string[] args)
{
Dictionary<String, Circle> circles =
new Dictionary<String, Circle>();
circles.Add("c1", new Circle("C1", 1.0));
circles.Add("c2", new Circle("C2", 2.0));
circles.Add("c3", new Circle("c3", 3.0));
foreach (KeyValuePair<String, Circle> kvp in circles)
{
String k = kvp.Key;
Circle c = kvp.Value;
Console.WriteLine("Circle {0} has radius {1}",
k, c.Radius());
}
Console.ReadLine();
}
30

Dictionary<K,V> Example

31

Using Key as Indexer


static void Main(string[] args)
{
Dictionary<String, Circle> circles =
new Dictionary<String, Circle>();
circles.Add("c1", new Circle("C1", 1.0));
circles.Add("c2", new Circle("C2", 2.0));
circles.Add("c3", new Circle("c3", 3.0));
Circle c = circles["c2"];
Console.WriteLine("Circle {0} has radius {1}",
c.Name(), c.Radius());
Console.ReadLine();
}

32

Indexer Example Running

33

About Dictionary<K,V>

Key class must have a compare for


equality operation.
Keys must be unique.

Attempting to Add an item with a key


already in the Dictionary will result in an
exception.

Can set entry with an existing key, using


indexer notation.
34

static void Main(string[] args)


{
Dictionary<String, Circle> circles =
new Dictionary<String, Circle>();

Adding with Existing Key

circles.Add("c1", new Circle("C1", 1.0));


circles.Add("c2", new Circle("C2", 2.0));
circles.Add("c3", new Circle("c3", 3.0));
Circle c = circles["c2"];
Console.WriteLine("Circle {0} has radius {1}",
c.Name(), c.Radius());

circles["c2"] = new Circle("New C2", 200.0);


c = circles["c2"];

Console.WriteLine("Circle {0} has radius {1}",


c.Name(), c.Radius());
Console.ReadLine();

35

Adding with Existing Key

36

Other Generic Container Classes

Linked List
(No array operations)
SortedDictionary
SortedList
Stack

See .NET documentation

37

You might also like