Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Anonymous Methods

C# Anonymous Methods

In C#, an anonymous function is a method without a name that can be defined and used in code.

We have already mentioned that a delegate is used to reference a method with the same signature as it. In other words, you can use a delegate object to invoke a method that can be referenced by the delegate.

Anonymous methods (Anonymous methods) Provides a technique for passing a code block as a delegate parameter.

In an anonymous method you do not need to specify a return type; it is inferred from the return statement within the method body.

Lambda Expressions

A lambda expression is a concise syntax for creating an anonymous function. They are commonly used in LINQ queries and delegates.

Syntax

(parameters) => expression

// 或

(parameters) => { statement; }

Example

// Example: defining a delegate using a lambda expression

Func

<

int

,

int

,

int

add

=

(

a, b

)

=>

a

b

;

Console

.

WriteLine

(

add

(

2

,

3

)

)

;

// Output 5

// Example: filtering elements in an array using a lambda expression

int

[

]

numbers

=

{

1

,

2

,

3

,

4

,

5

}

;

var

evenNumbers

=

numbers

.

Where

(

n

=>

n

%

2

==

0

)

;

foreach

(

var

num

in

evenNumbers

)

{

    Console

.

WriteLine

(

num

)

;

// Output 2 4

}

Anonymous methods

An anonymous method is declared by using the delegate keyword to create a delegate instance.

Syntax



delegate(parameters) { statement; }

For example:



delegate void NumberChanger(int n);

...

NumberChanger nc = delegate(int x)

{

    Console.WriteLine("Anonymous Method: {0}", x);

};



The code block Console.WriteLine("Anonymous Method: {0}", x); is the body of the anonymous method.

A delegate can be invoked through an anonymous method or through a named method, that is, by passing a method argument to the delegate object.

Note: The body of an anonymous method needs to be followed by a ;.

For example:



nc(10);

Example

// Example: defining a delegate using an anonymous method

Func

<

int

,

int

,

int

multiply

=

delegate

(

int

a,

int

b

)

{

return

a

b

;

}

;

Console

.

WriteLine

(

multiply

(

2

,

3

)

)

;

// Output 6

// Example: using an anonymous method as an event handler

Button button

=

new

Button

(

)

;

button

.

Click

+=

delegate

(

object

sender, EventArgs e

)

{

    Console

.

WriteLine

(

"Button clicked!"

)

;

}

;

Example

The following example demonstrates the concept of anonymous methods:

Example

using

System

;

delegate

void

NumberChanger

(

int

n

)

;

namespace

DelegateAppl

{

   

class

TestDelegate

   

{

       

static

int

num

=

10

;

       

public

static

void

AddNum

(

int

p

)

       

{

            num

+=

p

;

            Console

.

WriteLine

(

"Named Method: {0}"

, num

)

;

       

}

       

public

static

void

MultNum

(

int

q

)

       

{

            num

*=

q

;

            Console

.

WriteLine

(

"Named Method: {0}"

, num

)

;

       

}

       

static

void

Main

(

string

[

]

args

)

       

{

           

// Creating a delegate instance using an anonymous method

            NumberChanger nc

=

delegate

(

int

x

)

           

{

               Console

.

WriteLine

(

"Anonymous Method: {0}"

, x

)

;

           

}

;

           

           

// Invoking a delegate using an anonymous method

            nc

(

10

)

;

           

// Instantiating a delegate using a named method

            nc

=

 

new

NumberChanger

(

AddNum

)

;

           

           

// Invoking a delegate using a named method

            nc

(

5

)

;

           

// Instantiating a delegate using another named method

            nc

=

 

new

NumberChanger

(

MultNum

)

;

           

           

// Invoking a delegate using a named method

            nc

(

2

)

;

            Console

.

ReadKey

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



Anonymous Method: 10

Named Method: 15

Named Method: 30

In C# 2.0 and later, lambda expressions were introduced; they are a more concise syntax form for writing anonymous methods.

Using lambda expressions:

Example

using

System

;

delegate

void

NumberChanger

(

int

n

)

;

namespace

DelegateAppl

{

   

class

TestDelegate

   

{

       

static

int

num

=

10

;

       

public

static

void

AddNum

(

int

p

)

       

{

            num

+=

p

;

            Console

.

WriteLine

(

"Named Method: {0}"

, num

)

;

       

}

       

public

static

void

MultNum

(

int

q

)

       

{

            num

*=

q

;

            Console

.

WriteLine

(

"Named Method: {0}"

, num

)

;

       

}

       

static

void

Main

(

string

[

]

args

)

       

{

           

// Creating a delegate instance using a lambda expression

            NumberChanger nc

=

x

=>

Console

.

WriteLine

(

$

"Lambda Expression: {x}"

)

;

           

// Invoking a delegate using a lambda expression

            nc

(

10

)

;

           

// Instantiating a delegate using a named method

            nc

=

new

NumberChanger

(

AddNum

)

;

           

// Invoking a delegate using a named method

            nc

(

5

)

;

           

// Instantiating a delegate using another named method

            nc

=

new

NumberChanger

(

MultNum

)

;

           

// Invoking a delegate using a named method

            nc

(

2

)

;

            Console

.

ReadKey

(

)

;

       

}

   

}

}