Question: What is Delegate? Explain in brief.
Definition of Delegate:
Definition of Delegate:
(Entry 1 of 2): a person empowered to act on behalf of another: as. a : a person who is authorized to perform another's duties under a contract. b : a representative to a convention (as of a political party) or conference.
What is the synonym of delegate?
Delegate(n) Synonyms: deputy, proxy, vicar, commissioner, legate, envoy, appointee, surrogate
What is Delegate? Explain in brief.
- The Delegate is a function pointer we can say it is like a pointer to a function.
- It is a reference data type and it holds the reference of a function.
- All the delegates are implicity derived from System.Delegate namespace.
- In the .NET environment a delegate is a type that defines a method signature and it can pass a function as a parameter.
- In simple word we can say delegate is a .NET object which points to a method that matches its specific signature.
- A Delegate can be declared using delegate keyword followed by a function signature.
Syntax:
<access_modifier> delegate <return_Type> <delegate_name> (<parameters>)
Example:
Public delegate void delegatemethod(int n1);
- The delegatemethod delegate show above, can be used to print to any method that has same return type (void) & parameters declared in delegatemethod with int data type.
Example:
- using System;
- public delegate void Fprint();
- namespace Test
- {
- Class program
- {
- public Static void hello()
- {
- Console.WriteLine("hello all");
- Console.ReadLine();
- }
- public Static void main()
- {
- Fprint fp = new Fprint(hello);
- fp();
- }
- }
- }