ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

What is Anonymous methods ?How to use Delegate keyword?

.Net C# about 7 years ago || 11/20/2018 || 1.3 K View

  • Hide

Anonymous method  is a method which is introduced in C# from 3.0 and higher version. This  method uses delegate keyword to pass a block of code. By using this methods, we can  reduce the coding overhead in instantiating delegates because we do not have to create a separate method. Creating anonymous methods is  a way to pass a code block as a delegate parameter. 

An anonymous method is an inline method . It does not have a name.

Syntax for Anonymous method is given below:

// Create a delegate.
delegate void Del(int x);

// Instantiate the delegate using an anonymous method.
Del d = delegate(int y) { /* ..... */ };

Example of Anonymous method:

using System;

namespace Anonymous_Methods
{
    class Anonymous
    {
        delegate int Del(int l, int b); //Anonymous method is created using delegate keyword.
        static void Main(string[] args)
        {
         
            new Anonymous().Subtraction();     
            new Anonymous().Multiplication();
            new Anonymous().Addition();


        }  
        public void Subtraction()
        {
            
            Del sub = delegate (int length, int breadth)
            {
                return length - breadth;
            };
            Console.WriteLine("Subtraction is:");
            Console.WriteLine(sub.Invoke(10, 5).ToString()); 
        }
        public void Addition()
        {
           
            Del add = delegate (int length, int breadth)
            {
                return length + breadth;
            };
            Console.WriteLine("Addition is:");
            Console.WriteLine(add.Invoke(7, 4).ToString());
        }
        public void Multiplication()
        {
           
            Del A = delegate (int length, int breadth)
            {
                return length * breadth;
            };
            Console.WriteLine("Area of rectangle is:");
            Console.WriteLine(A.Invoke(5, 6).ToString());
        }
    }
}

Output of the above program is shown below.

Also read : What is TempData? How to store object in TempData?

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Manish Pandit

Manish Pandit

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey