O'Reilly logo

Learning C# 2005, 2nd Edition by Brian MacDonald, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 17: Delegates and Events

Quiz

Solution to Question 17–1.

The following is the standard way to define a delegate:

public delegate void PhoneRangHandler
( object sender, EventArgs e );

public event PhoneRangHandler PhoneRang;
Solution to Question 17–2.

Reference types.

Solution to Question 17–3.

To decouple the method(s) called from the calling code. It allows the designer of an object to define the delegate, and the user of the object to define which method will be called when the delegate is invoked.

Solution to Question 17–4.

You instantiate a delegate like this:

OnPhoneRings myDelegate = new OnPhoneRings(myMethod);
Solution to Question 17–5.

Here is how to call a delegated method:

OnPhoneRings(object this, new EventArgs(  ));
Solution to Question 17–6.

The ability for more than one method to be called through a single delegate.

Solution to Question 17–7.

Limits the use of the delegate in the following ways:

  • You can only add a method using +=.

  • You can only remove a method using -=.

  • The delegate can only be invoked by the class that defines it.

Solution to Question 17–8.

Define the delegate to take, as its second parameter, an object of a type derived from EventArgs. Pass the information through properties of that object.

Solution to Question 17–9.

None.

Solution to Question 17–10.

Rather than creating a method that matches the delegate’s signature and then assigning the name of that method to the delegate, you can directly assign an unnamed delegate method by providing the implementation in line ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required