Trong chương trước, bạn đã học được cú pháp cơ bản và làm thế nào để tạo ra các loại của riêng bạn. Điều này chương trong vòng yếu tố cần thiết để trần của những gì bạn cần biết các đại biểu và các sự kiện, giao diện, và giới thiệu một cách nhanh chóng đến các mảng và generics | This page intentionally left blank Chapter 4 Learning Just Enough C and Intermediate Syntax 89 90 Microsoft Visual Studio 2010 A Beginner s Guide Key Skills Concepts Use Delegates and Events Implement Interfaces Code with Arrays and Generics In 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