Microsoft 9GD00001 Computer Accessories User Manual


 
90 Microsoft Visual Studio 2010: A Beginner’s Guide
Key Skills & Concepts
Use Delegates and Events
Implement Interfaces
Code with Arrays and Generics
I
n previous chapters, you learned basic syntax and how to create your own types. This
chapter rounds out the bare essentials of what you need to know with delegates and
events, interfaces, and a quick introduction to arrays and generics. This material doesn’
t
attempt to be too advanced, but gives you enough information to understand the language
concepts involved. You’ll see all of these language features being used throughout the
book, and it’s good to have some background on what they mean. Let’s start off with
delegates and events.
Understanding Delegates and Events
Sometimes you need to write flexible code that performs general operations. For example,
when the designers of the .NET Framework created user interfaces, they added reusable
controls, such as buttons, list boxes, and grids. When writing these controls, the framework
designers didn’t know how we would use them. For example, how would anyone know
what we wanted our code to do when a user clicks a button on the user interface? So, these
controls have interaction points built in so that they can communicate with your program;
these interaction points are called events. These events fire whenever a user performs an
action such as a button click or a list box selection. We write code to hook up these events
to some other code in our program that we want to run when that event happens, such as
when the user clicks a button, and this is what delegates are used for.
An event defines the type of notifications that a object can provide, and a delegate
allows us to connect the event to the code we want to run.
This section will show you the mechanics of how delegates and events work, but you
should understand that the mechanics may seem somewhat abstract at first. Delegates and
events are most often used when you’re working with .NET Framework technologies that
use them, such as Windows Presentation Foundation (WPF), Silverlight, and ASP.NET.
What you’ll want to do is get a feel for the mechanics right now and then refer back to this
discussion when you encounter delegates and events in later chapters.