Documente Academic
Documente Profesional
Documente Cultură
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
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>
Li k dLi t T
LinkedList<T>
R
Represents
t a doubly
d bl lilinked
k d lilist.
t
List<T>
Queue<T>
SortedDictionary<TKey,TValue>
SortedList<TKey,TValue>
Stack<T>
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Generic Collections 4
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
;
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Generic Collections 8
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Generic Collections 9
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
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