Skip to main content

Delegates

  • Chapter
Illustrated C# 2010
  • 739 Accesses

Abstract

A delegate is a user-defined type, like a class. But whereas a class represents a collection of data, a delegate keeps track of one or more methods. You use a delegate by doing the following. We’ll go through each of these steps in detail in the following sections.

  1. 1.

    Declare a new delegate type with a particular signature and return type. A delegate declaration looks like a method declaration, except that it doesn’t have an implementation block.

  2. 2.

    Declare a delegate variable of the new delegate type.

  3. 3.

    Create an object of the delegate type, and assign it to the delegate variable. The new delegate object includes a reference to a method with the same signature as defined in the first step.

  4. 4.

    Add additional methods into the delegate object. These methods must have the same signature and return type as the delegate type defined in the first step.

  5. 5.

    Throughout your code you can then invoke the delegate, just as it if it were a method. When you invoke the delegate, each of the methods it contains is executed.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 49.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2010 Daniel M. Solis

About this chapter

Cite this chapter

Solis, D.M. (2010). Delegates. In: Illustrated C# 2010. Apress. https://doi.org/10.1007/978-1-4302-3283-4_15

Download citation

Publish with us

Policies and ethics