Home > C# and dot NET > Anonymous method C#

Anonymous method C#

Anonymous method adalah method tanpa nama, hanya block body method. Kita tidak perlu menspesifikasikan return type dari anonymous method tersebut karena return type disimpulkan secara implisit dari return statement.
Anonymous method menyediakan teknik untuk passing code-block sebagai parameter delegates. Anonymous method di deklarasikan saat inisiasi instance delegates dengan keyword delegate.

<delegate-name> <delegate-object-name> = <delegate-keyword> ()
{
/* Method body*/
};

Example

namespace AppBasicConsole
{
delegate int NumberChanger(int inputNumber);
delegate string StringReverse(string inputString);

class Program
{

static void Main(string[] args)
{

NumberChanger numberChanger = delegate(int inputNumber)
{
return inputNumber * inputNumber;
};

StringReverse reverse = delegate(string inputString)
{
char[] arrCharacter = inputString.ToCharArray();
Array.Reverse(arrCharacter);
return new string(arrCharacter);
};

Console.WriteLine(numberChanger(5));
Console.WriteLine(reverse("Hello, Goodbye"));

}
}
}

Semoga bermanfaat.

Referensi : dari berbagai sumber.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: