Factorial function means to multiply a series of descending natural numbers.
Example: 5! = 5x4x3x2x1 =125.
ConsoleApplication has been given a reference of Class Library (FactLIB).
Here the Class Library (FactLIB) is used to write the logic of factorial.
It will be easy to get the result for small numbers, but what if I had to find the factorial of large number like 10k?
The program at runtime will be on a pause mode.
To Handle this problem we have a concept called Delegates.
Delegate is defined as pointer to a function.
It is a communication channel which helps us to call back after getting the results.
A delegate can invoke the method to which it refers. Thus, the method that will be invoked by a delegate is not determined at compile time, but rather at runtime.
The ganeral form of delegate declaration is shown here:
public delegate <return-type> delegatename(input parameter);
➢ return-type is the type of value returned by methods that the delegate will be calling.
➢ The parameters required by the methods called through the delegates are specified in the parameter-list or input parameter.
- There are 4 steps to create and use the delegate
Step 1: Declare a delegate
Step 2: Create a Delegate reference
Step 3: Point the Refernce pointer to Method
Step 4: Invoke the methods through Delegate
➢ Here BigInteger data-type is used to display huge numbers came from the namespace using.Numerics;
➢ Thread is used for parallel execution. Here thread is used to run the Main method and Calculate method at the same time.
There are two types of Delegates:
- Single Delegate:
- A single delegate is called simple delegate it invokes a single method.
- Simple delegate refer to a single method.
- Multicast Delegate:
- It can invoke multiple methods.
- Multicast delegates can send messages to multiple clients/subscribers.
- It uses += sign to broadcast multiple clients.
- It makes 2 way communication.
Also see another .NET OOPS interview questions video on Events and Delegates in C#: -