top
April flash sale

Search

C# Tutorial

Anonymous methods are unnamed methods in a code that can be defined using the delegate keyword. They only require a body and not a name or a return type. Anonymous methods allow users to write inline codes rather than explicit methods. Their behaviour is similar to normal methods.Salient Points of Anonymous MethodsSome of the salient points of Anonymous methods are as follows:Anonymous methods are defined using the delegate keyword.Anonymous methods can be used in event handling.Any unsafe codes cannot be accessed inside anonymous methods.Variables declared outside the anonymous methods can be accessed inside them.Variables declared inside the anonymous methods cannot be accessed outside them.Anonymous methods can be passed as parameters.A program that demonstrates Anonymous methods is given as follows:Source Code: Program that demonstrates Anonymous methods in C#using System; namespace AnonymousMethodDemo {   class Example {      public delegate void sum(int x, int y);      static void Main(string[] args)      {         sum s = delegate(int x, int y)         {           Console.WriteLine("Inside Anonymous method");           Console.WriteLine("Sum = {0}",x + y);         };         s(7, 12);      }   } }The output of the above program is as follows:Inside Anonymous method Sum = 19Limitations of Anonymous MethodsSome of the limitations of Anonymous methods are given as follows:Any unsafe codes cannot be accessed inside anonymous methods.There cannot be jump statements such as goto, break or continue inside anonymous methods.Anonymous methods cannot be used on the left side of the is operator.
logo

C# Tutorial

Anonymous Methods in C#

Anonymous methods are unnamed methods in a code that can be defined using the delegate keyword. They only require a body and not a name or a return type. Anonymous methods allow users to write inline codes rather than explicit methods. Their behaviour is similar to normal methods.

Salient Points of Anonymous Methods

Some of the salient points of Anonymous methods are as follows:

  1. Anonymous methods are defined using the delegate keyword.
  2. Anonymous methods can be used in event handling.
  3. Any unsafe codes cannot be accessed inside anonymous methods.
  4. Variables declared outside the anonymous methods can be accessed inside them.
  5. Variables declared inside the anonymous methods cannot be accessed outside them.
  6. Anonymous methods can be passed as parameters.

A program that demonstrates Anonymous methods is given as follows:

Source Code: Program that demonstrates Anonymous methods in C#

using System;
namespace AnonymousMethodDemo
{
  class Example {
     public delegate void sum(int x, int y);
     static void Main(string[] args)
     {
        sum s = delegate(int x, int y)
        {
          Console.WriteLine("Inside Anonymous method");
          Console.WriteLine("Sum = {0}",x + y);
        };
        s(7, 12);
     }
  }
}

The output of the above program is as follows:

Inside Anonymous method
Sum = 19

Limitations of Anonymous Methods

Some of the limitations of Anonymous methods are given as follows:

  1. Any unsafe codes cannot be accessed inside anonymous methods.
  2. There cannot be jump statements such as goto, break or continue inside anonymous methods.
  3. Anonymous methods cannot be used on the left side of the is operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial