Collections Interfaces

19
 Collections Interfaces Chapter 7: Collections Interfaces Objectives Thi s cha pter discusses about col lect ions in C# and how to wor k wit h dif fer ent collection classes along with IEnumerable, IEnumerator and IComparable interfaces. At the end of this chapter delegate would be in a position to accomplish the following activities. What is a collection and different interfaces what collections are implementing ow to Work Arra!"ist, ueue, $tack, ashTa ble and $orted"ist Classes What are IEnumerable and IEnumerator interfaces and h ow to use them. Wo rking with IComparable interface

description

C

Transcript of Collections Interfaces

Page 1: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 1/19

Collections Interfaces

Chapter 7: Collections Interfaces

Objectives

This chapter discusses about collections in C# and how to work with different

collection classes along with IEnumerable, IEnumerator and IComparable interfaces. At

the end of this chapter delegate would be in a position to accomplish the following

activities.

What is a collection and different interfaces what collections are implementing

ow to Work Arra!"ist, ueue, $tack, ashTable and $orted"ist Classes

What are IEnumerable and IEnumerator interfaces and how to use them.

Working with IComparable interface

Page 2: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 2/19

Collections Interfaces

Introduction

The most primitive container within the .%ET platform is the System.Array t!pe.

As !ou have seen over the course of the previous chapters, C# arra!s allow !ou to define

a set of identicall! t!ped items &including an arra! of $!stem.'b(ects, which essentiall!

represents an arra! of an! t!pes) of a fi*ed upper limit. While this will often fit the bill,there are man! other times where !ou re+uire more fle*ible data structures, such as a

d!namicall! growing and shrinking container, or a container that can hold onl! items that

meet a specific criteria &e.g., onl! items deriving from a given base class, items

implementing a particular interface, or what not).

To begin understanding the task of building fle*ible and t!pesafe containers, this

chapter will first e*amine the System.Collections namespace that has been part of the

.%ET base class libraries since the initial release. The .%ET -ramework provides

specialied classes for data storage and retrieval. These classes provide support for 

stacks, +ueues, lists, and hash tables. /ost collection classes implement the sameinterfaces, and these interfaces ma! be inherited to create new collection classes that fit

more specialied data storage needs.

The Interfaces of the System.Collections Namespace:

The $!stem.Collections namespace defines a number of interfaces. A ma(orit! of the

classes within $!stem.Collections implement these interfaces to provide access to their

contents. 0elow table gives a breakdown of the core collectioncentric interfaces.

System.Collections

Interface

Meanin

ICollection 1efines general characteristics &e.g., sie, numeration,

thread safet!) for all collection t!pes.

IComparer Allows two ob(ects to be compared.

I1ictionar! Allows a collection ob(ect to represent its contents using

name2value pairs.

IEnumerable 3eturns the IEnumerator interface for a given ob(ect.

IEnumerator Enables foreach st!le iteration of subt!pes.

IashCode4rovider 3eturns the hash code for the implementing t!pe using a

customied hash algorithm.I"ist 4rovides behavior to add, remove, and inde* items in a list

of ob(ects. Also, this interface defines members to determine

whether the implementing collection t!pe is readonl!

and2or a fi*edsie container.

Page 3: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 3/19

Collections Interfaces

/an! of these interfaces are related b! an interface hierarch!, while others are

standalone entities. 0elow figure illustrates the relationship between each t!pe.

The Class Types of System.Collections:

System.Collections

Class

Meanin !ey Interfaces

Implemented

Arra!"ist 3epresents a d!namicall! siedarra! of ob(ects.

I"ist, ICollection, andIEnumerable.

ashTable 3epresents a collection of

'b(ects identified b! a

 %umerical ke!.

I1ictionar!, ICollection,

and IEnumerable.

ueue 3epresents -I-' ueue ICollection and

IEnumerable.

$orted "ist "ike a dictionar!. The elements

Can also accessed b! position

I1ictionar!, ICollection,

and IEnumerable.

$tack 3epresents "I-' ueue ICollection and

IEnumerable.

Array "ist

• The $!stem.Collections.Arra!"ist class is similar to arra!s, but can store elements

of an! data t!pe.

Page 4: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 4/19

Collections Interfaces

•  We don5t need to specif! the sie of the collection when using an Arra!"ist &as

we used to do in the case of simple arra!s).

• The sie of the Array"ist  grows d!namicall! as the number of elements it

contains changes.

• An Array"ist uses an arra! internall! and initialies its sie with a default value

called Capacit!.

•  As the number of elements increase or decrease, Array"ist ad(usts the capacit!

of the arra! accordingl! b! making a new arra! and cop!ing the old values into it.

• The $ie of the Array"ist is the total number of elements that are actuall! present

in it while the Capacit! is the number of elements the Array"ist can hold without

instantiating a new arra!.

• An Array"ist can be constructed like this6

Array"ist demoA" # ne$ Array"ist%&'

E*ample 76

class 4rogram

  8

  static void /ain&string9: args)

  8

  Arra!"ist demoAl ; new Arra!"ist&)<

  22=et the number of Elements

  Console.Write"ine&>%o.of elements in the arra!list6 8?@ >, demoAl.Count)<

  Console.Write"ine&>Adding elements>)<

  demoAl.Add&)<

  demoAl.Add&>i>)<

  demoAl.Add&B.D7)<

  demoAl.Add&>ello>)<

  22=et the number of Elements

  Console.Write"ine&>%o.of elements in the arra!list6 8?@ >, demoAl.Count)<

  Console.Write"ine&>3emoving elements>)<

  22 3emove elements from the arra! list.

  demoAl.3emove&)<

  22 Capacit! of the arra! list

  Console.Write"ine&>Current capacit!6 > demoAl.Capacit!)<

Page 5: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 5/19

Collections Interfaces

  22Change arra! contetns using Arra!Inde*

  Console.Write"ine&>Change first three elements>)<

  demoAl9?: ; 5F5<

  demoAl97: ; 5G5<

  demoAl9D: ; 5H5<

E*ampleD6

class 4rogram

  8

  static void /ain&string9: args)

  8

  22 create an arra! list

Arra!"ist al ; new Arra!"ist&)<

  22 Add elements to the arra! list

al.Add&7)<

  al.Add&B7)<

  al.Add&B7)<

  al.Add&J7J)<

  al.Add&7)<

  al.Add&7K7)<

 

Console.Write&>'riginal contents6 >)<

  foreach &ob(ect i in al)

  Console.Write&i > >)<

  Console.Write"ine&>Ln>)<

  22 $ort

al.$ort&)<

  22 Mse foreach loop to displa! the list.

Console.Write&>Contents after sorting6 >)<

  foreach &ob(ect i in al)  Console.Write&i > >)<

  22 Cop!

  string9: arra!7 ; new string97?:<

  al.Cop!To&arra!7, ?)<

  Console.Write"ine&>Arra! 76>)<

Page 6: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 6/19

Collections Interfaces

  foreach &string s in arra!7)

  8

  Console.Write"ine&>Lt8?@>, s)<

  @

  22 3everse

  al.3everse&)<

  foreach &string s in al)

  8

  Console.Write"ine&>Lt8?@>, s)<

  @

  al.TrimTo$ie&)<

Queue:

ueues are containers that ensure items are accessed using a firstin, firstout manner.

$adl!, we humans are sub(ect to +ueues all da! long6 lines at the bank, lines at the movie

theater, and lines at the morning coffeehouse. When !ou are modeling a scenario in

which items are handled on a firstcome, firstserved basis, $!stem.Collections.ueue fits

the bill. In addition to the functionalit! provided b! the supported interfaces, ueue

defines the ke! members which are given below.

(e)ueue %&6 3emoves and returns the ob(ect at the beginning of the ueue

*n)ueue %&:  Adds an ob(ect to the end of the ueue

+ee, %&6 3eturns the ob(ect at the beginning of the ueue without removing it

Example 1: Enqueue & DeQueue

namespace ueueNE*ample7

8

  class 4rogram

  8

  static void /ain&string9: args)

  8  ueue + ; new ueue&)<

  +.En+ueue&7)<

  +.En+ueue&D)<

  +.En+ueue&)<

  +.En+ueue&B)<

Page 7: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 7/19

Collections Interfaces

  Console.Write&>+ueue6 >)<

  foreach &int i in +)

  Console.Write&i > >)<

  Console.Write"ine&)<

  Console.Write&>1e+ueue O >)<

  int a ; &int)+.1e+ueue&)<

  Console.Write"ine&a)<

  Console.Write&>+ueue6 >)<

  foreach &int i in +)

  Console.Write&i > >)<

  Console.Write"ine&)<

  Console.Write"ine&>The 4eek element is >)<

  Console.Write"ine&+.4eek&))<

+.Clear&)<

@

  @

}

1o note that if !ou attempt to remove items from an empt! +ueue, a runtime e*ception is

thrown.

The big difference of the +ueue is that the interface I"ist is not implemented. Gou cannot

access the +ueue using an inde*er

Stac,:

• The $!stem.Collections.$tack class is a kind of collection that provides controlled

access to its elements.

•  A stack works on the principle of "ast In -irst 'ut &"I-O), which means that the

last item inserted into the stack will be the first item to be removed from it.

• $tacks and ueues are ver! common data structures in computer science and the!

are implemented in both hardware and software.

• The insertion of an item onto the stack is termed as a 5+ush5 while removing an

item from the stack is called a 5+op5.

Page 8: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 8/19

Collections Interfaces

• If the item is not removed but onl! read from the top of the stack, then this is

called a 5+ee, 5 operation.

The $tack class can be instantiated in a manner similar to the one we used for the

Arra!"ist

Stac, demoStac, # ne$ Stac,%&'

E*ample76

class 4rogram

  8

  static void /ain&string9: args)

  8

  $tack 1emo$tack ; new $tack&)<

  1emo$tack.4ush&B)<

  1emo$tack.4ush&>i>)<

  1emo$tack.4ush&D.B)<

  1emo$tack.4ush&>ello>)<

  Console.Write"ine&>4rinting the elements in the stack>)<

  foreach &ob(ect o in 1emo$tack)

  8

  Console.Write&o >Lt>)<

  @

  Console.Write"ine&)<  Console.Write"ine&>The no.of elements in the stack is > 1emo$tack.Count)<

  ob(ect a ; 1emo$tack.4op&)<

  Console.Write"ine&>The no.of elements in the stack is > 1emo$tack.Count)<

  Console.Write"ine&1emo$tack.4eek&))<

  1emo$tack.Clear&)<

  @

  @

(ictionary

• 1ictionaries are a kind of collection that store items in a ke!value pair fashion.

• Each value in the collection is identified b! its ke!.

• All the ke!s in the collection are uni+ue and there cannot be more than one ke!

with the same name.

Page 9: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 9/19

Collections Interfaces

• This is similar to the English language dictionar! like the '*ford 1ictionar!

where each word &ke!) has its corresponding meaning &value).

• The two most common t!pes of 1ictionaries in the $!stem.Collections namespace

are the ashtable and the $orted"ist.

Hash Table:

This session helps us to understand how the hash table collection works. A hash table is a

data structure that associates ke!s with values. The 0ase Class "ibraries offer a

ashTable class that is defined in the $!stem.Collections namespace so that !ou are not

re+uired to code !our own hash tables. When an element is added to the ashtable, the

element is placed into a bucket based on the hash code of the ke!. $ubse+uent lookups of

the ke! use the hash code of the ke! to search in onl! one particular bucket, thus

substantiall! reducing the number of ke! comparisons re+uired to find an element.

0ecause hashing eliminates the need for costl! searching of data to retrieve the data, !oucan use hashing to efficientl! retrieve data.

In computer science, a hash table or hash map is a data structure that uses a hash

function to efficientl! map certain identifiers or ke!s &e.g., person names) to associated

values &e.g., their telephone numbers).

Constructin a ashtable 

There are constructors available to instantiate a hashtable<

ashtable ht # ne$ ashtable%&'

E*ample6

class 4rogram

  8

  static void /ain&string9: args)

  8

  ashtable ht ; new ashtable&)<

  ht.Add&>%ame>, >$reekanth>)<

  ht.Add&>$alar!>, 7????)<

  ht.Add&>1ept%o>, )<

  Console.Write"ine&>%o.'f elements in the hashtable is6> ht.Count)<

  Console.Write"ine&>All the ke!s in the ashTable are 6>)<

  foreach &ob(ect o in ht.Pe!s)

Page 10: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 10/19

Collections Interfaces

  8

  Console.Write"ine&o >Lt>)<

  @

  Console.Write"ine&>ash Table is fi*ed Q > ht.Is-i*ed$ie)<

  Console.Write"ine&>All the values in the ashTable are 6>)<

  foreach &ob(ect o in ht.Ralues)

  8

  Console.Write"ine&o >Lt>)<

  @

  ht.3emove&>$alar!>)<

  Console.Write"ine&>All the ke!s in the ashTable are 6>)<

  foreach &ob(ect o in ht.Pe!s)

  8

  Console.Write"ine&o >Lt>)<

  @  22ht.Add&>1ept%o>, D)<

  Console.Write"ine&>Ln value;> ht9>1ept%o>:)<

  if &ht.Contains&>%ame>))

  8

  Console.Write"ine&>Pe! E*ists>)<

  @

  else

  8

  Console.Write"ine&>Pe! doesnot e*ists>)<

  @ 

@

  @

SortedList:

The Sorted"ist class represents a collection of ke!andvalue pairs that are sorted b! the

ke!s and are accessible b! ke! and b! inde*. A Sorted"ist is a h!brid between a

ashtable and an Arra!. When an element is accessed b! its ke! using the Item inde*er

 propert!, it behaves like a ashtable. When an element is accessed b! its inde* using

=et0!Inde* or $et0!Inde*, it behaves like an Arra!. If Count alread! e+uals Capacit!,

the capacit! of the Sorted"ist ob(ect is increased b! automaticall! reallocating the

internal arra!, and the e*isting elements are copied to the new arra! before the new

element is added.

Page 11: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 11/19

Collections Interfaces

Example 1:

class 4rogram

  8

  static void /ain&string9: args)

  8  $orted"ist 1emo$" ; new $orted"ist&)<

  1emo$".Add&7, >I>)<

  1emo$".Add&D, >ello>)<

  1emo$".Add&, >ow>)<

  1emo$".Add&B, >Are !ou>)<

  Console.Write"ine&>Capacit! of the $orted"ist is 8?@>,1emo$".Capacit!)<

  Console.Write"ine&1emo$".ContainsRalue&>ow>))<

  Console.Write"ine&1emo$".=et0!Inde*&D))<

  Console.Write"ine&>The items in the sorted order6Ln>)<

  for &int i ; ?< i S 1emo$".Count< i)

  8

  Console.Write"ine&>Lt 8?@ LtLt87@>, 1emo$".=etPe!&i),

1emo$".=et0!Inde*&i))<

  @

  1emo$"9D: ; >ello =ood morning><

  Console.Write"ine&1emo$".=et0!Inde*&7))<

 

@  @

Example 2:

class 4rogram

  8

  static void /ain&string9: args)

  8

  $orted"ist m!$orted"ist ; new $orted"ist&)<

  m!$orted"ist.Add&>%G>, >%ew Gork>)<

  m!$orted"ist.Add&>-">, >-lorida>)<

  m!$orted"ist.Add&>A">, >Alabama>)<

  m!$orted"ist.Add&>WG>, >W!oming>)<

  m!$orted"ist.Add&>CA>, >California>)<

  foreach &string m!Pe! in m!$orted"ist.Pe!s)

Page 12: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 12/19

Collections Interfaces

  8

  Console.Write"ine&>m!Pe! ; > m!Pe!)<

  @

  foreach &string m!Ralue in m!$orted"ist.Ralues)

  8

  Console.Write"ine&>m!Ralue ; > m!Ralue)<

  @

  Console.Write"ine&>=etting the ke! list>)<

  I"ist m!Pe!"ist ; m!$orted"ist.=etPe!"ist&)<

  foreach &string m!Pe! in m!Pe!"ist)

  8

  Console.Write"ine&>m!Pe! ; > m!Pe!)<

  @

  Console.Write"ine&>=etting the value list6>)<  I"ist m!Ralue"ist ; m!$orted"ist.=etRalue"ist&)<

  foreach &string m!Ralue in m!Ralue"ist)

  8

  Console.Write"ine&>m!Ralue ; > m!Ralue)<

  @

 

@

  @

Inde/ers:

Inde*ers are location indicators and are used to access class ob(ects, (ust like accessing

elements in an arra!. An inde*er looks like a propert! and is written the same wa! a

 propert! is written, but with two differences6

• The inde*ers takes an inde* argument and looks like an arra!.

• The inde*er is declared using the name this.

The inde*er is implemented through et and set accessors for the 9: operator.

4ublic double this 94arameter:8

get

8

22 =et codes goes here

@

set

Page 13: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 13/19

Collections Interfaces

  8

22 $et codes goes here

@

@

The implementation rules for the et and set accessors are the same as for properties. The

return t!pe determined what will be returned.

*/ample:

class 4arentClass

  8

  private string9: range ; new string9:<

  public string this9int inde*range:

  8

  get

  8

  return range9inde*range:<

  @

  set

  8

  range9inde*range: ; value<

  @

  @

  @

  2 The Above Class (ust act as arra! declaration using this pointer 2

  class childclass

  8

  public static void /ain&)

  8

  4arentClass ob( ; new 4arentClass&)<

  2 The Above Class 4arentClass create one ob(ect name is ob( 2

  ob(9?: ; >'%E><

  ob(97: ; >TW'><

  ob(9D: ; >T3EE><

  ob(9: ; >-'M3 ><

  ob(9B: ; >-IRE><

  Console.Write"ine&>WE"C'/E T' C# C'3%E3 '/E 4A=ELn>)<

  Console.Write"ine&>Ln>)<

Page 14: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 14/19

Collections Interfaces

  Console.Write"ine&>8?@Ln,87@Ln,8D@Ln,8@Ln,8B@Ln>, ob(9?:, ob(97:, ob(9D:, ob(9:,

ob(9B:)<

  Console.3ead"ine&)<

  @

  @

Mse inde*ers onl! when it makes sense, when the representation of the class data as

multiple ob(ects is proper.

As pointed out earlier, inde*ers and properties are ver! similar in concept, but differ in

the following wa!s6

A propert! can be static member, whereas an inde*er is alwa!s an instancemember.

• A get accessor of a propert! corresponds to a method with no parameters, whereas

a get accessor of an inde*er corresponds to a method with same formal parameter

list as the inde*er.

• A set accessor of a propert! corresponds to a method with a single parameter

named value, whereas a get accessor of an inde*er corresponds to a method with

same formal parameter list as the inde*er, plus the parameter named value.

*numerable Types:

IEnumerable, IEnumerator interfaces, which facilitates the iterative access in a custom

collection. Mnlike C, .%ET -ramework facilitates accessing individual elements in the

custom collection while implementing IEnumerable and IEnumerator interfaces. -or

e*ample, it is a hassle free (ob to access t!pes collected in an Arra!"ist. This had been

eased b! Arra!"ist class while implementing IEnumerable and IEnumerator interfaces.

The IEnumerable interface contains an abstract member function called =etEnumerator&)

and return an interface IEnumerator on an! success call. This IEnumerator interface will

allow us to iterate through an! custom collection.

$ee the figure. This is how it works.

Page 15: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 15/19

Collections Interfaces

Members of I*numerator Interface

0eset%&1 MoveNe/t%&1 Current%&

The signature of IEnumerator members is as follows6

  void 0eset%&:This method returns a void t!pe and helps !ou to position the

 pointer (ust before the start point. This reset method will also help !ou to reset theiteration pointer an!where from the start position.

• bool MoveNe/t%&:This method will help !ou to position the location of the

re+uired element in the collection while sending a flag value. And its ob(ective is

to inform the caller whether the current position holds an! value or not. If it has,then /ove%e*t will return true or return false in case there is no value.

• object Current: The ob(ect Current propert! of IEnumerator interface will return

an individual element of a given collection. This propert! is used to return an

element in the collection b! using the specified location pointer.

If !ou are tr!ing to enumerate for the first time, then !ou need to call methods and

 properties in this hierarch!6 3eset&) O /ove%e*t &) O Current.

-rom second time on$ards the hierarchy is : /ove%e*t &) O Current.

*/ample:

 public class $impleCollection 6 IEnumerable

  8

Page 16: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 16/19

Collections Interfaces

  public $impleCollection&ob(ect9: arra!)

  8

  items ; arra!<

  @

  public IEnumerator =etEnumerator&)

  8

  return new Enumerator&items)<

  @

  private ob(ect9: items ; null<

  @

  public class Enumerator 6 IEnumerator 

  8

  private int cursor<

  private ob(ect9: elements ; null<  public Enumerator&ob(ect9: items)

  8

  elements ; new ob(ect9items."ength:<

  Arra!.Cop!&items, elements, items."ength)<

  cursor ; 7<

  @

  public bool /ove%e*t&)

  8

  cursor<

  if &cursor O &elements."ength 7))  8

  return false<

  @

  return true<

  @

 

 public void 3eset&)

  8

  cursor ; 7<

  @

  public ob(ect Current

  8

  get

  8

  return elements9cursor:<

  @

Page 17: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 17/19

Collections Interfaces

  @

  @

  public class $tarter 

  8

  public static void /ain&)

  8

  $impleCollection simple ; new $impleCollection&new ob(ect9: 8 7, D, , B, , , U

@)<

  IEnumerator enumerator ; simple.=etEnumerator&)<

  while &enumerator./ove%e*t&))

  8

  Console.Write"ine&enumerator.Current)<

  @

  @

Comparable Objects:

The $!stem.IComparable interface specifies a behavior that allows an ob(ect to be

sorted based on some specified ke!. 1efines a generalied comparison method that a

value t!pe or class implements to create a t!pespecific comparison method.

To enable !our classes to be able to support custom comparisons, !our class needs to

implement the IComparable interface. IComparable contains a single method,

CompareTo&),that !our class will have to implement. The CompareTo&) method compares

the current instance of an ob(ect to another ob(ect of the same t!pe.

public interface IComparableThe formal definition is6

 public interface IComparable

8

int CompareTo&ob(ect o)<

@

E*ample6

 public class Task 6 IComparable

  8

  public int I1, 4riorit!<

  public Task&int pI1, int p4riorit!)

  8

  I1 ; pI1<

  4riorit! ; p4riorit!<

Page 18: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 18/19

Collections Interfaces

  @

  public int CompareTo&'b(ect ob()

  8

  Task $omeTask ; &Task)ob(<

  return this.4riorit! $omeTask.4riorit!<

  @

  @

  class 4rogram

  8

  static void /ain&string9: args)

  8

  3andom 3nd ; new 3andom&)<

  Arra!"ist Tasks ; new Arra!"ist&)<  for &int i ; 7< i S; < i)

  Tasks.Add&new Task&i, 3nd.%e*t&7??)))<

Console.Write"ine&>Mnsorted6LrLnI1 4riorit!>)<

  foreach &Task t in Tasks)

  Console.Write"ine&> > t.I1 > > t.4riorit!)<

  Console.Write"ine&)<

  22 $ort the tasks

  Tasks.$ort&)<

  Console.Write"ine&>$orted6LrLnI1 4riorit!>)<

  foreach &Task t in Tasks)  Console.Write"ine&> > t.I1 > > t.4riorit!)<

  @

  @

+oints to 0emember

 The C Colle!tion !lasses are a set o" !lasses desi#ned spe!i$!all% "or

#roupin# to#ether obe!ts and per"ormin# tas's on them(

A ma(orit! of the classes within $!stem.Collections implement interfaces to

 provide access to their contents.

Array"ist class is similar to arra!s, but can store elements of an! data t!pe.

The sie of the Array"ist grows d!namicall! as the number of elements it

Page 19: Collections Interfaces

7/18/2019 Collections Interfaces

http://slidepdf.com/reader/full/collections-interfaces 19/19

Collections Interfaces

contains changes.

2ueues are containers that ensure items are accessed using a firstin, firstout

manner.

If !ou attempt to remove items from an empty )ueue, a runtime e*ception is

thrown. Stac,  works on the principle of "ast In -irst 'ut &"I-O), which means that the

last item inserted into the stack will be the first item to be removed from it.

A hash table is a data structure that associates ke!s with values.

When an element is added to the ashtable, the element is placed into a bucket

 based on the hash code of the ke!.

The Sorted"ist class represents a collection of ke!andvalue pairs that are sorted

 b! the ke!s and are accessible b! ke! and b! inde*.

 A Sorted"ist is a h!brid between a ashtable and an Arra!.

Inde/ers are location indicators and are used to access class ob(ects, (ust like

accessing elements in an arra!. I*numerable, I*numerator interfaces facilitates the iterative access in a custom

collection.

The System.IComparable interface specifies a behavior that allows an ob(ect to

 be sorted based on some specified ke!.

 

Chec, 3ourSelf 

*/ercise:

Write a 4rogram to implement hast Table ob(ects as elements in $orted "ist.

Write the differences between ash Table and $orted "ist.

Illustrate the use of Custom Collections and write a 4rogram to implement it.

Write a 4rogram to convert an Arra!"ist to Arra!.

Write a 4rogram to show the differences between $orted"ist and

$orted1ictionar!.