Defining a Delegate

Syntax

[<modifiers>] delegate void|type Name( [<param definitions>] )


Note:

while declaring a delegate you should follow the same syntax as in the method i.e. IO parameters of delegate should be same as the IO parameters of method we want to call using the delegate.


public void AddNums(int x, int y) {
	Console.WriteLine(x + y);
}
public delegate void AddDel(int x, int y);
public static string SayHello(string name) {
	return “Hello “ + name;
}
public delegate string SayDel(string name);

Example
using System;
namespace OOPSProject {
	public delegate void AddDel(int a, int b, int c);
	public delegate string SayDel(string name);
	public delegate void MathDel(int x, int y);
}
Add a new class DelDemo.cs under the project and write the following code:
class DelDemo {
	public void AddNums(int x, int y, int z) {
		Console.WriteLine(x + y + z);
	}
	public static string SayHello(string name) {
		return "Hello " + name;
	}
	static void Main() {
		DelDemo obj = new DelDemo();
		AddDel ad = obj.AddNums;
		SayDel sd = DelDemo.SayHello;
		ad(80, 40, 29);
		ad(189, 645, 640);
		ad(127, 485, 349);
		Console.WriteLine(sd("Hello"));
		Console.WriteLine(sd("Scanftree"));
		Console.WriteLine(sd("Hi"));
		Console.ReadLine();
	}
}

Quantitative Aptitude
Reasoning
Programming
Interview