Professional Documents
Culture Documents
Chapte R 19 & 20
Chapte R 19 & 20
Enumerating collections
After completing this chapter, you will be
able to:
In the next exercise, you will define a class that implements
the generic IEnumerator<T> interface and create an
enumerator for the binary tree class that is demonstrated in
Chapter 17,
1. Create the TreeEnumerator class
2. In the Code and Text Editor window displaying the
TreeEnumerator.cs file, modify the definition of the
TreeEnumerator class to satisfy these requirements
3. Add the three private variables shown in the following
code in bold to the TreeEnumerator <TItem> class:
This method walks the binary tree, adding the data it contains to the queue. The main
difference is that rather than the method appending NodeData values to a string, it stores
these values in the queue.
Update the body of the MoveNext method with the
code shown in bold here:
the MoveNext method should just move through data
items until there are no more left, dequeuing items
until the queue is empty, as in this example.
It is important to keep in mind that MoveNext does not
actually return data items—that is the purpose of the
Current property.
All MoveNext does is update the internal state in the
enumerator (that is, the value of the currentItem
variable is set to the data item extracted from the
queue) for use by the Current property, returning true
if there is a next value and false otherwise.
The Current property examines the enumData variable to ensure that
MoveNext has been called. (This variable will be null prior to the first
call to MoveNext.)
If this is not the case, the property throws an InvalidOperationException
—this is the conventional mechanism used by .NET Framework
applications to indicate that an operation cannot be performed in the
current state.
If MoveNext has been called beforehand, it will have updated the
currentItem variable, so all the Current property needs to do is return the
value in this variable.
Implementing an enumerator by using an iterator
An iterator is a block of code that yields an ordered
sequence of values.
• Event Handlers in the .NET Framework return void and take two parameters.
• The first paramter is the source of the event; that is the publishing object.
• The second parameter is an object derived from EventArgs.
• Events are properties of the class publishing the event.
• The keyword event controls how the event property is accessed by the subscribing
Declaring Events
event delegateTypeName
eventName
tempMonitor.MachineOverheating += welder.FinishWelding;
tempMonitor.MachineOverheating += painter.PaintOff;
Raising an event
You can raise an event, just like a delegate, by calling it
like a method.
When you raise an event, all the attached delegates are
called in sequence.
For example, here’s the TemperatureMonitor class with a
private Notify method that raises the MachineOverheating
event:
Understanding user interface events
C# Button Control
Windows Forms controls are reusable components that
encapsulate user interface functionality and are used in
client side Windows applications.
A button is a control, which is an interactive
component that enables users to communicate with an
application.
The Button class inherits directly from the ButtonBase
class.
A Button can be clicked by using the mouse, ENTER
key, or SPACEBAR if the button has focus.