Generic Delegates in C# With Examples

In the previous article, we have learned about the delegates and their types. Now in this article, we are going to discuss the ‘generic delegates in C#‘.

What are the generic delegates?

The  Func ,  Action  and  Predicate  are the three generic delegates which were introduced in .NET 3.5 and are availables in System namespace.

We can make use of all these three predefined generic delegates when we want to invoke the methods without defining the delegates explicitly.

Func delegate in C#

Func generic delegates in c#

Func is a generic delegate that is available in the System namespace.

It can take up to 16 input parameters of the same or different data types and one out parameter.

Continue reading “Generic Delegates in C# With Examples”

C# – Anonymous Method with examples

What is Anonymous Method?

 Anonymous method  : In C#, An anonymous method is a method without name and return type.

It can be created using the delegate keyword with the optional parameters and can be assigned to a variable of the delegate type.
An anonymous method is an inline statement or expression which makes the code easy and more readable.

An anonymous method is useful when we want to hook an action directly to an event without separately creating an event handler.

Syntax to declare an anonymous method

The following is the syntax to declare an anonymous method in C#.

    // Code

Continue reading “C# – Anonymous Method with examples”

Delegates in C# | Multicast Delegate with Examples

delegate in c#

In C#, delegates are reference types available in the system namespace.
A delegate is a type-safe function pointer that can contain one or more references to methods with the same signature and return type as the delegate. Delegates are used to pass methods as parameters to other methods. Event handlers are nothing more than methods called through delegates.
The delegate in C # is very similar to the function pointer in C and C ++, but unlike the function pointer in C, the delegate is object-oriented, type-safe, and secure.

Delegates in C# – Overview

Continue reading “Delegates in C# | Multicast Delegate with Examples”