Introduction
In this article I will explain Delegates and how to them in a F# console application.
What is Delegate?
A Delegate is a special function that encapsulate a method and represents a method signature. Delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the address of functions. The reference can be changed at runtime. Delegates are used for implementing events and call-back functions. A delegate is referred to as type-safe function pointers, in other words if the method with two int parameters and a return type is an int then the delegate that you are declaring should also have the same signature. That is why a delegate is referred to as type-safe function.
The following is the syntax to declare a Delegate:
type delegate-typename=delegate of type1 ->type2
The following are the 3 ways to define Delegates:
- Declaration
- Instantiation
- Invocation
The following are the 2 types of Delegates:
- Simple Delegate
- Multicast Delegate
Multicast Delegate
A Multicast Delegate holds references of one or more functions. Multicast delegates must contain only methods that have the return type void only, else there is a runtime exception. The Delegate object uses the "+" and "-" operator s. The "+" operator composes the number of delegates by the delegate object and the "-" operator removes the component delegate from the composed delegate object.
Features
The following are the features of Delegates:
- Delegate are like function pointers but type safe.
- Delegates allow methods to be passed as a parameter.
- A Delegate can be used for callback methods.
- Delegates improve the performance of the application.
Invoke( ) Function
The Invoke( ) Function executes the specified delegate with the specified list of arguments.
Let's use the following procedure to understand Delegates.
Step 1:
Open Visual Studio then click "Create New Project" --> "F# Console Application".
![create-app.jpg]()
Step 2:
Write the following code in the F# application.
#light
open System
type DemoDelegates() =
static member add(a : int, b : int) =
a + b
static member sub (a : int) (b : int) =
a - b
member x.Add(a : int, b : int) =
a + b
member x.Sub (a : int) (b : int) =
a - b
type Delegateobj1 = delegate of (int * int) -> int
type Delegateobj2 = delegate of int * int -> int
let InvokeDelegate1 (dlg : Delegateobj1) (a : int) (b: int) =
dlg.Invoke(a, b)
let Delegateobj2 (dlg : Delegateobj2) (a : int) (b: int) =
dlg.Invoke(a, b)
let del3 : Delegateobj1 = new Delegateobj1( DemoDelegates.add )
let del4 : Delegateobj2 = new Delegateobj2( DemoDelegates.sub )
let delobj = DemoDelegates()
let del1 : Delegateobj1 = new Delegateobj1( delobj.Add )
let del2 : Delegateobj2 = new Delegateobj2( delobj.Sub )
for (a, b) in [ (50,40 ); (0,0) ] do
printfn "\nAdd:%d + %d = %d" a b (InvokeDelegate1 del1 a b)
printfn "\nSub:%d - %d = %d" a b (Delegateobj2 del2 a b)
printfn "\nadd:%d + %d = %d" a b (InvokeDelegate1 del3 a b)
printfn "\nsub:%d - %d = %d" a b (Delegateobj2 del4 a b)
System.Console.ReadKey(true)
Output
Debug the application by pressing F5 and the result will be in the application as shown in the following figure.
![output.jpg]()
Summary
This article explained Delegates and how to use them in a F# console application and you saw how to declare and implement the delegates.