Delegate
Steps for using delegation:
1. Define Delegation
2. instantiate the delegate
3. Use Delegation
[Access modifier] delegate name of the delegate return type ();
Class Program
{
// Define the delegate call
Public Delegate int call (INT num1, int num2 );
Class math
{
Public int multiply (INT num1, int num2)
{
// Statement;
}
Public int divide (INT num1, int num2)
{
// Statement;
}
}
Class testdelegates
{
Static void main ()
{
Call objcall;
Math objmath = new math ();
// Instantiate the delegate call
Objcall = new call (objmath. Multiply );
}
}
}
Call delegate
Calling a delegate means that a delegate is used to instantiate a method.
Class delegates
{
// Delegate Definition
Public Delegate int call (INT num1, int num2 );
Class math
{
// Multiplication method
Public int multiply (INT num1, int num2)
{
Return num1 * num2;
}
// Division method
Public int divide (INT num1, int num2)
{
Return num1/num2;
}
}
Class testdelegates
{
Static void main ()
{
// The variable that stores the result
Int result;
// Delegate object
Call objcall;
// Math class Object
Math objmath = new math ();
// Instantiate the delegate
Objcall = new call (objmath. Multiply );
// Call the delegate
Result = objcall (5, 3 );
Console. writeline (result );
}
}
}
Console output:
15
Define events
[Access modifier] event Delegate name event name;
When defining an event, the publisher first defines the delegate and then defines the event according to the delegate.
// Define a delegate
Public Delegate void delegateme ();
// Define an event
Private event delegateme eventme;
Why define an event with a delegate? Here, delegates are used to limit the type of the function triggered by the event, that is, the number of parameters of the function, the type, and the type of the return value of the function.
Subscribe to eventsOnly a delegate is added. When an event is triggered, the Delegate will call a method.
// The obja method subscribes to the event eventme
Eventme + = new delegateme (obja. method );
Events
Checks specific conditions. If the specified condition is true, the event eventme is triggered.
If (true)
{
// Trigger eventme event
Eventme ();
}
Example:
Class student
{
// Define the delegate
Public Delegate void delegateregisterfinish ();
// Define an event through delegation
Public event delegateregisterfinish registerfinish;
Private string _ name;
Public student (string name)
{
_ Name = Name;
}
Public void register ()
{
Console. writeline ("Student {0} register", _ name );
If (registerfinish! = NULL)
{
// Events
Registerfinish ();
}
}
}
Class registe
{
Static void main ()
{
Console. writeline ("enter the name of the registered student :");
String studentname = console. Readline ();
Student = new student (studentname );
// Subscribe to the registerfinish event of the Student Class
Student. registerfinish + = new student. delegateregisterfinish (student_regis );
Student. Register ();
}
Private Static void student_regis ()
{
Console. writeline ("registered successfully ");
}
}
Console output:
Enter the name of the registered student:
Tom
Student Tom Registration
Registered successfully
Exercise:
Create the event zeroentered and accept the two numbers entered by the user. If the number is 0, this event should be triggered and the method disp should be called.
Show "do not allow Division by zero". If the input number is greater than 0, divide the number and display the result.
Class Test
{
// Define a delegate
Public Delegate void delegateme ();
// Define an event
Public event delegateme zeroentered;
Public void register (INT num1, int num2)
{
If (num2 = 0)
{
// Events
Zeroentered ();
}
Else
{
Console. writeline (num1/num2 );
}
}
}
Class Tests
{
Static void main ()
{
Console. writeline ("Enter divisor ");
Int num = int. parse (console. Readline ());
Console. writeline ("input divisor ");
Int num2 = int. parse (console. Readline ());
Test OBJ = new test ();
OBJ. zeroentered + = new test. delegateme (disp );
OBJ. Register (Num, num2 );
}
Public static void disp ()
{
Console. writeline ("Division by Zero is not allowed ");
}
}
First time
Console output:
Input Divisor
10
Input Divisor
5
2
Second
Console output:
Input Divisor
10
Input Divisor
0
It is not allowed to use zero as the divisor.