Finished Defining a Delegate & Using Multi-cast Delegate

Jason Zhu 2021-02-22 21:37:17 +11:00
parent fb25385807
commit 24b1c07128

@ -3,14 +3,21 @@
## Overloading Methods ## Overloading Methods
Reference: Reference:
* [Method signature](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods#method-signatures)
* [Member Overloading](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading) * [Member Overloading](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading)
* [Method signatures in C#](https://www.c-sharpcorner.com/uploadfile/puranindia/method-signatures-in-C-Sharp/)
Member overloading means creating two or more members on the same type (same return type) that differ only in the number or type of parameters but have the same name. Because only methods, constructors, and indexed properties can have parameters, only those members can be overloaded. Member overloading means creating two or more members on the same type (same return type) that differ only in the number or type of parameters but have the same name. Because only methods, constructors, and indexed properties can have parameters, only those members can be overloaded.
Method signature: Methods, constructors, indexers, and operators are characterized by their signatures. A signature makes a method look unique to the C# compiler.
Method signature consists:
* Method name * Method name
* Parameter types, & number of paramter * Parameter types, & number of paramter
Method signature DOES NOT include:
* return type/kind
## Defining Properties ## Defining Properties
Reference: Reference:
@ -64,3 +71,102 @@ Some class members are decalred to be readonly. There are multiple methods can a
* `const` value can be assigned with `public` access modifier, as it cannot be modified * `const` value can be assigned with `public` access modifier, as it cannot be modified
* Normally `const` values are CAPITAL. * Normally `const` values are CAPITAL.
* `const` value is treaded as `static` (i.e. belong to class, instead of object as it cannot be changed) * `const` value is treaded as `static` (i.e. belong to class, instead of object as it cannot be changed)
## Introducing Events and Delegate
* Events is not in style with server frameworks
* Events are hard to understand
* Events are popular in **forms** and desktop programming
* Events build on top of **delegates**
## Defining a Delegate
Reference:
* [Early binding and late binding in C++](https://www.learncpp.com/cpp-tutorial/early-binding-and-late-binding/)
* [Introduction to Delegates](https://docs.microsoft.com/en-us/dotnet/csharp/delegates-overview)
* [委托C# 编程指南)](https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/delegates/)
* Best explain with e.g.: [Delegates in C#](https://dotnettutorials.net/lesson/delegates-csharp/)
### What is Delegate
In some programs, it is not possible to know which function will be called until runtime (when the program is run). This is known as late binding (or dynamic binding). In C++, one way to get late binding is to use function pointers. In C#, we can get late binding using **delegate**
Methods, constructors, indexers, and operators are characterized by their signatures.
> In simple words, we can say that the delegates in C# are the **Type-Safe Function Pointer**. It means they hold the reference of a method or function and then calls that method for execution.
### Steps to define & use Delegates
To invoke or call a method using delegates, 3 steps are required:
1. Defining a delegate
2. Instantiating a delegate
3. Invoking a delegate
#### Step 1: Defining a delegate
Syntax (outside of class):
```
<Access Modifier> delegate <return type> <delegate name> (arguments list);
```
e.g. for method (within class)
```csharp
public void Add(int x, int y)
{
Console.WriteLine(@"The Sum of {0} and {1}, is {2} ", x, y, (x + y));
}
```
we have delegate defined as
```csharp
public delegate void AddDelegate(int a, int b);
```
#### Step 2: Instantiating the Delegate
To consume the delegate you must first create an object of the delegate. While creating the object using defined delegate, you should parse the method you want to execute as a parameter
Syntax:
```csharp
DelegateName ObjectName = new Delegate_Name (target_function_name);
```
e.g.
```csharp
AddDelegate ad = new AddDelegate(obj.Add);
```
#### Step 3: Invoking the delegate
Now call the delegate by supplying the required values to the parameters so that the methods (bound with the delegates) get executed internally.
e.g.
```csharp
ad(100, 50);
```
Rules of using Delegates in C#:
* A delegate in C# is a user-defined type and hence before invoking a method using delegate, we must have to define that delegate first.
* The signature of the delegate must match the signature of the method, the delegate points to otherwise we will get a compiler error. This is the reason why delegates are called as type-safe function pointers.
* A Delegate is similar to a class. Means we can create an instance of a delegate and when we do so, we need to pass the method name as a parameter to the delegate constructor, and it is the function the delegate will point to
* Tip to remember delegate syntax: Delegates syntax look very much similar to a method with a delegate keyword.
## Using Multi-cast Delegates
Reference:
* [Delegate and MulticastDelegate classes](https://docs.microsoft.com/en-us/dotnet/csharp/delegate-class#delegate-and-multicastdelegate-classes)
* Best explain with e.g.: [Multicast Delegates in C# with Examples](https://dotnettutorials.net/lesson/multicast-delegate-csharp/#:~:text=A%20Multicast%20Delegate%20is%20a,signature%20should%20be%20the%20same.)
A Multicast Delegate is a delegate that holds the references of more than one function. When **mutliple methods having the same signature** so we can create a single delegate that holds the reference of the these methods.
* use += operator to chain delegates together and -= operator to remove.
* A multicast delegate invokes the methods in the invocation list, in the same order in which they are added.
Overall, delegate enable us to declare a method that used like a variable.