Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Anonymous function in C#

 What is anonymous function in C#?

An anonymous function is an "inline" statement or expression that can be used wherever a delegate type is expected.

You can use it to initialize a named delegate or pass it instead of a named delegate type as a method parameter.

You can use a lambda expression or an anonymous method to create an anonymous function. We recommend using lambda expressions as they provide more concise and expressive way to write inline code.

Reference…https://docs.microsoft.com/

How to use anonymous function in C#?

Anonymous function code examples using,

  • Delegate with named method,
  • Lambda expression
  • Delegate with anonymous method
  • Built-in delegates (Func & Action delegates)


delegate void SampleDelegate(string x);

    

public class AnonymousFunctions
    {
         static void DisplayData(string x)
        {
            Console.WriteLine("Data is :" + x);
        }
        static void AnonymousSamples()
        {
            // initialization with a named method.
            SampleDelegate testNamed = new SampleDelegate(DisplayData);
 
            // C# 2.0: A delegate can be initialized with
            // inline code, called an "anonymous method." This
            // method takes a string as an input parameter.
            SampleDelegate TestAnMethod = delegate (string s) { Console.WriteLine(s); };
 
            // C# 3.0. A delegate initialization with a lambda expression.
            // The lambda also takes a string
            // as an input parameter (x). The type of x is inferred by the compiler.
            SampleDelegate TestLambdaExp = (x) => { Console.WriteLine(x); };
 
            //Anonymous function with action generic delegate
            Action<string> TestDelGeneric = (s) => { Console.WriteLine(s); };
 
            // Anonymous function with action delegate without paremeters.
            Action TestDelActGen = () => { Console.WriteLine("Codechef4U samples"); };
 
            //Anonymous Function with func generic delegate, first 2
            // parameters are input, and last parameter is result
            Func<string, string, string> GetCombinedString = (x, y) => { return x+ " " + y; };
 
            //Anonymous Function with predicate generic delegate
            Predicate<string> isValidSurName = (x) => { return x != null && x.Length >2 ? true : false; };
 
            // Invoke the delegates.
            DisplayData("The named method call using delegate .");
            TestAnMethod("The anonymous method call.");
            TestLambdaExp("The lambda call.");
            TestDelGeneric("The action generic delegate .");
            TestDelActGen();
            Console.WriteLine("Full name is: "+ GetCombinedString("Aditya", "Kendre"));
            Console.WriteLine("Is valid name :" + isValidSurName("Kendre"));
        }
    } 

Invalid entry,please enter valid data.

Loading