Вы находитесь на странице: 1из 7

C# 30

Generic Collections 1

C# 3.0
Chapter 20 Generic Collections

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 2

Introduction
The BCL provides
pro ides collection classes to
groups
p of objects
j
in-memory
y
hold g
Each collection type is unique by representing its own
data structure to hold objects
Collections are encapsulated in such a way that the
underlying data structure cant
can t be altered

The .NET Framework 2.0 introduces


generic Enumerators and an iterator
mechanism

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 3

Generic Collections
Type

Description

Dictionary<TKey,TValue>

Represents a collection of keys and values.

Li k dLi t T
LinkedList<T>

R
Represents
t a doubly
d bl lilinked
k d lilist.
t

List<T>

Represents a strongly typed list of objects


that can be accessed by index
index.

Queue<T>

Represents a first-in, first-out collection of


objects.

SortedDictionary<TKey,TValue>

Represents a collection of key/value pairs


that are sorted on the key.

SortedList<TKey,TValue>

Represents a collection off key/value


/
pairs
that are sorted by key.

Stack<T>

Represents a variable size last-in-first-out


last in first out
(LIFO) collection of instances of the same
arbitrary type.

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 4

Using Generic Collections


Dictionary<string,Customer>customers=new
Dictionary<string,Customer>();
customers.Add("Meg",newCustomer("Meg","Ryan"));
customers.Add("George",newCustomer("George",
"Clooney"));
"Cl
"))
customers.Add("Brad",newCustomer("Brad","Pitt"));
Customercustomer customers["Brad"];
Customercustomer=customers["Brad"];
//Iteratorsareusedtoiterateagenericcollection
IEnumerable<Customer>iterator=customers Values;
IEnumerable<Customer>iterator=customers.Values;
List<Customer>temp=newList<Customer>(iterator);
temp Sort(Customer FirstName);
temp.Sort(Customer.FirstName);

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 5

Iterators
Iterators are generic types used to iterate
an encapsulated Collection
Each one of the Generic Collections is
provided with an Iterator
An iterator can be used as the body
y of a
method, operator, or get accessor
The return type of an iterator must be one
of the following interfaces:
IEnumerable,IEnumerator,IEnumerable<T>,
IEnumerator<T>
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 6

Iterator Types
There are two types of an iterator:
Unnamed iterator (most common)
Named iterator
SolarSystemPlanets planets=newSolarSystemPlanets();
//Unnamediterator
foreach (stringplanetinplanets)
Console WriteLine(planet);
Console.WriteLine(planet);
//Namediterator (iterator method)
foreach (stringplanetinplanets.Iterator(3,6))
(stringplanetinplanets Iterator(3 6))
Console.WriteLine(planet);
//Namediterator (throughaproperty)
SolarSystem solarSystem =newSolarSystem();
foreach (stringplanetinsolarSystem.Planets)
Console.WriteLine(planet);
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 7

Creating an Iterator
Iterators specify the return values by using
the yieldreturn statement
publicIEnumerator GetEnumerator()
{
foreach (stringplanetinplanets)
y
yieldreturnplanet;
p
;
}

Alternatively, you can implement IEnumerable and


IEnumerator by hand much more tedious!

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 8

Creating an Iterator (cont


(cont.))
publicclassSolarSystemPlanets:Ienumerable
{
privatestring[]_planets= ...;
publicIEnumeratorGetEnumerator()
bli IE
t G tE
t ()
{
foreach(stringplanetin planets)
foreach(stringplanetin_planets)
yieldreturnplanet;
}
publicIEnumerable<string>Iterator(
bli IE
bl
t i
It
t (
int start,intend)
{
for(inti=start;i<=end;++i)
yieldreturn_planets[i];
}
}

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 9

Generic Type Iterator


publicclassGenericCollection<T>
{
privateT[]_items;
publicGenericCollection(int count)
{
_items=newT[count];
}
...
publicIEnumerator GetEnumerator()
{
foreach (Titemin_items)
{
i ld t
it
yieldreturnitem;
}
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 10

Collection Initializers
Arrays
native
convenient
Arra s have
ha e a nati
e and con
enient
initialization syntax
y
Why not any collection?
int[]firstPrimes
i
t[]fi tP i
={2,3,5,7};
{2 3 5 7}
List<int>nums =newList<int>{
5,3,20
5 3 20
};
Dictionary<string,int>dict =
newDictionary<string,int>{
{A,1},
{
,
}, Becomes
{B,2}
};
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 11

Collection Initializers Notes


The pattern works
orks for an
any ICollection<T>
IC ll ti
T
It calls Add(T) for each element

It also works for any IEnumerable that also


has an Add method
If you have
h
a range off elements
l
to add,
dd
prefer built
built-in
in AddRange methods
(performance)
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 12

Chapter 20 Exercise 1

IMPLEMENTING A GENERIC
COLLECTION
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Generic Collections 13

Summary
Generic collections are efficient
efficient, ttype
pe safe
data structures
Collections and types can be iterated with
th C# foreach
the
f
h keyword
k
d using
i an iterator
it t
The iterator code specifies
p
how return values are
generated when the foreach loop accesses each
element of the collection
Iterators simplify the process of implementing
IEnumerable or IEnumerator methods
Iterators allowing you to concentrate on writing the
iteration code rather implementing IEnumerator
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Вам также может понравиться