What is .Net Delegates
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 words we can say delegate is a .NET object which points to a method that matches its specific signature. A delegate is a form of type-safe function pointer used by the Common Language Infrastructure.
You create a delegate with the delegate keyword, followed by a return type and the signature of the methods that can be delegated to it.
A delegate is independent of the type of method that it references. The signature of the method and the delegate should match. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
There are two types of Delegates
1) Singlecast delegates
2) Multiplecast delegates
A Singlecast delegate point to single method at a time and it is derived from System.Delegate class.
A delegate that can have more than one element in its invocation list is known as a multicast delegate. When you create a Delegate object, you actually create an instance of an object that inherits the System.MulticastDelegate class. The MulticastDelegate class lets you assign multiple methods to a delegate.
What is the use of delegates in C#
The following examples shows how to implement delegates in .Net programs
- Difference between a Debug and Release build
- Difference between normal DLL and .Net DLL
- What is an Interface
- Difference between Abstract Class and Interface
- Difference between a thread and a process
- Differences between a control and a component
- Differences between Stack and Heap
- What is .Net Reflection
- Globalization and Localization
- What is .Net serialization
- Difference between web service and .net remoting
- Difference between managed and unmanaged code
- Difference between Shallow copy and Deep copy
- Use of System.Environment Class