CSE459_CSharp_07_Enumeration

CSE459_CSharp_07_Enumeration - Programming in C#...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming in C# Collections CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Collection Namespaces l The .NET framework provides several components that aid in using collections, either those directly implemented in .NET or your own custom collections. l The collection namespaces are: Namespace Contains System.Collections Nongeneric collection classes and interfaces. System.Collections.Specialized Strongly typed nongeneric collection classes. System.Collections.Generic Generic collection classes and interfaces. System.Collections.ObjectModel Proxies and base classes for custom collections.
Background image of page 2
Flavors of Collections l Scattered across these namespaces are components that: l Provide standard interfaces for working with a broad range of collection implementations. l Provide robust and complete concrete implementations for common data structures such as linked-lists, dynamic arrays and partial maps. l Provide wrappers around a few of these concrete classes for user customization.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Generic vs. non-Generic l The Generic collection interfaces and classes provide strong type safety and should almost always be preferred over the non-generic collections introduced in C# 1.0 l Many of the generic interfaces are derived from the non-generic interfaces, so we will cover both.
Background image of page 4
What is a Collection? l A collection can provide various degrees of functionality. l At a minimum it needs to provide some mechanism to list or enumerate its members. l Other useful, but non-essential functionality might include adding and removing items, clearing the collection, indexing into the collection, …
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Enumeration l Basic construct of any enumerator: public interface IEnumerator { bool MoveNext(); object Current { get ; } void Reset(); }
Background image of page 6
Enumeration l Basic construct of any enumerator: public interface IEnumerator { bool MoveNext(); object Current { get ; } void Reset(); } This is one of the beauties of type unification. Question and an action Called before Current All interfaces in .NET have an initial letter of I
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bad Enumerator Design l Using an enumerator is quite easy: class MyCollection : IEnumerator { … } MyCollection animals; animals.Reset(); while (animals.MoveNext()) { Animal pet = (Animal) animals.Current; As we will see shortly, the foreach statement provides a much cleaner implementation.
Background image of page 8
l Using an enumerator is quite easy: class MyCollection : IEnumerator { … } MyCollection animals; animals.Reset(); while (animals.MoveNext()) { Animal pet = (Animal) animals.Current; Non-generic requires a type conversion. As we will see shortly, the foreach
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CSE 459.24 taught by Professor Crawfis during the Winter '11 term at Ohio State.

Page1 / 39

CSE459_CSharp_07_Enumeration - Programming in C#...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online