0% found this document useful (0 votes)
139 views

Delegates (C# Programming Guide)

The document discusses delegates in C# programming. Delegates allow methods to be passed as parameters and used as callbacks. Delegates are like function pointers but are type safe. Methods can be passed to other methods through delegates. Events are also handled through delegates. The document provides an overview and examples of using delegates.

Uploaded by

Vishal Panwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Delegates (C# Programming Guide)

The document discusses delegates in C# programming. Delegates allow methods to be passed as parameters and used as callbacks. Delegates are like function pointers but are type safe. Methods can be passed to other methods through delegates. Events are also handled through delegates. The document provides an overview and examples of using delegates.

Uploaded by

Vishal Panwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/27/2016

Delegates(C#ProgrammingGuide)

Delegates C# Programming Guide


Visual Studio 2015

A delegate is a type that represents references to methods with a particular parameter list and return type. When you
instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You
can invoke or call the method through the delegate instance.
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods
that are invoked through delegates. You create a custom method, and a class such as a windows control can call your
method when a certain event occurs. The following example shows a delegate declaration:
C#
publicdelegateintPerformCalculation(intx,inty);

Any method from any accessible class or struct that matches the delegate type can be assigned to the delegate. The
method can be either static or an instance method. This makes it possible to programmatically change method calls, and
also plug new code into existing classes.

Note
In the context of method overloading, the signature of a method does not include the return value. But in the context
of delegates, the signature does include the return value. In other words, a method must have the same return type
as the delegate.
This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. For example, a
reference to a method that compares two objects could be passed as an argument to a sort algorithm. Because the
comparison code is in a separate procedure, the sort algorithm can be written in a more general way.

Delegates Overview
Delegates have the following properties:
Delegates are like C++ function pointers but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
https://msdn.microsoft.com/enIN/library/ms173171.aspx

1/3

4/27/2016

Delegates(C#ProgrammingGuide)

Methods do not have to match the delegate type exactly. For more information, see Using Variance in
Delegates C# and Visual Basic.
C# version 2.0 introduced the concept of Anonymous Methods, which allow code blocks to be passed as
parameters in place of a separately defined method. C# 3.0 introduced lambda expressions as a more concise
way of writing inline code blocks. Both anonymous methods and lambda expressions in certain contexts are
compiled to delegate types. Together, these features are now known as anonymous functions. For more
information about lambda expressions, see Anonymous Functions C# Programming Guide.

In This Section
Using Delegates C# Programming Guide
When to Use Delegates Instead of Interfaces C# Programming Guide
Delegates with Named vs. Anonymous Methods C# Programming Guide
Anonymous Methods C# Programming Guide
Using Variance in Delegates C# and Visual Basic
How to: Combine Delegates Multicast DelegatesC# Programming Guide
How to: Declare, Instantiate, and Use a Delegate C# Programming Guide

C# Language Specification
For more information, see the C# Language Specification. The language specification is the definitive source for C#
syntax and usage.

Featured Book Chapters


Delegates, Events, and Lambda Expressions in C# 3.0 Cookbook, Third Edition: More than 250 solutions for C# 3.0
programmers
Delegates and Events in Learning C# 3.0: Master the fundamentals of C# 3.0

See Also
Delegate
https://msdn.microsoft.com/enIN/library/ms173171.aspx

2/3

4/27/2016

Delegates(C#ProgrammingGuide)

C# Programming Guide
Events C# Programming Guide

2016 Microsoft

https://msdn.microsoft.com/enIN/library/ms173171.aspx

3/3

You might also like