What is .Net Delegates
Delegates in C#
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
Differences between events and delegates
Delegate and Event concepts are completely tied together. Even if they were exactly the same thing there will still remain the semantical difference. Delegates is a function pointer which can able to store the address of any function with same prototype. While Event is a function handler which can handles or run the functions in same prototype of its delegate. More.... Events and Delegates
- C# Frequently Asked Questions - 1
- C# Frequently Asked Questions - 2
- C# Frequently Asked Questions - 3
- Difference between a Debug and Release build
- Difference between normal DLL and .Net DLL
- What is an Interface in C#
- 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
- What is the difference between private and shared assembly?
- Does the .NET have in-built support for serialization?
- How to properly stop the Thread in C#?
- Why are there five tracing levels in System.Diagnostics.TraceSwitcher?
- Why is XmlSerializer so slow?
- How many types of Jit Compilers?