Anonymous Method with C#

Anonymous Method with C#

Anonymous Method

As name suggests, anonymous method is a function without having a name. You can say code block with a name, and this can be assigned to a variable of delegate type and defined as delegate.

Before understanding anonymous methods, we need to understand delegate first. Let’s consider below example

?

?

?

To work with delegates, we followed the steps below.

1.????? Declare delegate with parameters


2.????? Implement method which need to be pointed from delegate


3.????? Create delegate object and pass method name in constructor of delegate and invoke method.

?

Above example, you saw that we are passing/binding function to delegate “PrintHello”, we can also bind a code block to a delegate.


In the above code, you can see that we have written an in-line code block which needs to be executed once delegate will be invoked. In this case, as you can see, instead of a method, we are binding the delegate to an unnamed code block which is also called an anonymous method and in C#, the anonymous methods are created by using the delegate keyword and if the anonymous method requires any input parameter, then you can pass the parameter within the parenthesis.?

?

Let’s look at another example of anonymous method. Suppose we have a list of employees, and we need to find if employee exist into database.

We can implement this using below code

?

?

In the above example, you can see we have first implement method, IsEmployeeExist, then we define Predicate generic delegate type and we pass method name.

?

We can achieve this using anonymous method using just one line of code

?

?

?

?

?

要查看或添加评论,请登录

Satya Prakash Chhikara的更多文章

  • Difference between RPC and REST

    Difference between RPC and REST

    Difference between RPC and REST Remote Procedure Call (RPC) and REST are two most followed architecture in API design…

  • Checksum

    Checksum

    Checksum Checksum is the outcome of running an algorithm, called a cryptographic hash function on a piece of data…

  • Proxy Server

    Proxy Server

    Proxy Server Definition A proxy server is a router that provides a gateway between users and the internet. It prevents…

  • Lambda Expressions in C#

    Lambda Expressions in C#

    Lambda Expressions in C# Lambda Expressions are shorthand writing for anonymous methods. Let’s look at anonymous method.

  • Generic Delegates

    Generic Delegates

    Generic Delegate Before discussing generic delegates, it takes an example of delegates. Delegate is a pointer to an…

  • Multicast Delegate

    Multicast Delegate

    Multicast Delegate Multicast Delegate is a delegate which holds references of more than a function pointer having same…

  • Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C# Events, delegates, events args and event handler are quite…

  • Finalize and Dispose in C#

    Finalize and Dispose in C#

    Finalize and Dispose in C# Finalize method 1. This method is used to release resources before the current object is…

  • Stack and Heap Memory in .NET

    Stack and Heap Memory in .NET

    Stack and Heap Memory in .NET Let’s try to understand what exactly happens when we declare a variable into .

  • Choosing between SQL and NoSQL

    Choosing between SQL and NoSQL

    Choosing between SQL and NoSQL When it comes to choosing database between SQL and NoSQL, it’s become tough and…

社区洞察

其他会员也浏览了