Anonymous Method In C#

Introduction

Hi Everyone. Today, I would like to share information about the Anonymous Method.

Description

The name itself describes it as "Anonymous". It means a method that does not have any name. We need not specify any return type for Anonymous method. For your understanding, I will give one small example, as shown below.

Example

  1. Delegate(Student s).  
  2. Class student {  
  3.     Public int Id {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     Public string Name {  
  8.         get;  
  9.         set;  
  10.     }  
  11. }  
The class student has 2 entities - Id, Name.
  1. Delegate bool IsCheck(Student s)  
  2. Class student _Extend {  
  3.     List < student > objstudents = new list < student > () {  
  4.         New student {  
  5.             ID = 1, Name = "Vetri"  
  6.         }  
  7.     };  
  8. }  
In the above class, I described the list which is defined by student type. Now, I need to query the list. The best option is LINQ. I will query the list by standard query or method.
  1. IsCheck objChk = delegate(student s) { return s.id>=1;};   
I created the object for delegate and queried the list and returned the boolean value.

Conclusion: So, this was a small example of Anonymous method.