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”

What is a delegate in C#? Multicast Delegate with Examples

delegate in c#

C# Delegates with Examples

In C#, a  Delegate  is a reference type that is available in system namespace.

A delegate is a wrapper of a function, or simply we can say that it is a Typesafe function pointer which can hold the reference of one or more methods of the same signature and return type.

Syntax to define a delegate in C#

In C#, The declaration of a delegate is very similar to a method signature. It can also have parameters and the return type as a normal method.

Following is the syntax to declare a delegate.

<AccessModifier> delegate <ReturnType> <DelegateName>(<Parameters>)

Continue reading “What is a delegate in C#? Multicast Delegate with Examples”