Building XNA Games- P2: I would like to acknowledge John Sedlak, who saved this book from certain doom, as well as all of the great guys in the XNA community and Microsoft XNA team, who helped me with all of my stupid programming questions. (That is actually the term used—“stupid programming question”—and it is a question that one should not have to ask if one has been approached to write a book about the subject.) | CHAPTER 1 A .NET SNAPSHOT 15 There is one major flaw with how we have been creating our arrays so far they aren t easy to modify. There is no simple way to add new elements or remove old ones. The next section describes how to use generics to create modifiable lists of a certain type. Using Generics and Events Generics or template classes are a way of defining a type such as a class based on another type. This relationship is known as the of a relationship because you can say that you are declaring a variable as a List of Box objects where List is the generic class and Box is the class you are using. Let s define our own little box collection using generics. First we need to create a class to handle items in a collection. Start up a new console application in Visual Studio and add a class called ListBase. Now we need to add the necessary namespaces and declare the class type. We use the letter T to signify the generic type but you can use any other letter. In fact it is possible to declare multiple generic types for a class. using System using namespace Generics class ListBase T List T Note that we do not do much with the generic parameter we simply pass it on up to the base class. Now we need to create some functionality in this class. One problem with the built-in generic collection types List Dictionary Queue Stack and so on is that they do not have events for when an item is added or removed. An event is a way of notifying code outside the class that something has happened. For example in a Windows application the window fires a MouseDown event whenever the user clicks the mouse on the window. This allows us to know about and handle events as they happen. To create an event we use what is known as a delegate which is nothing more than a function turned into a type. Here is the delegate we will be using defined in public delegate void EventHandler object sender EventArgs e You should notice right away that much of this looks