Home > C# and dot NET > Action C#

Action C#

Action adalah delegate yang mempunyai zero, satu atau lebih input parameter dan mengembalikan void. Kita dapat menggunakan Action untuk merepresentasikan method yang dapat di-pass sebagai parameter tanpa mendeklarasikan delegate secara explisit.

Syntax

delegate void Action();
delegate void Action(T arg);
delegate void Action(T1 arg1, T2 arg2);
 .. and so on, up to T16

Example

using System;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;

namespace AppBasicConsole
{
class Program
{
public static void Consumer(IEnumerable<string> names,  Action<string> consumerResolver)
{
if (consumerResolver == null) throw new ArgumentNullException("Exception is thrown");

foreach(string name in names)
{
consumerResolver(name);
}
}

static void Main(string[] args)
{
IEnumerable<string> names = new[]{"Jack", "Wilson", "Catherine", "Rina", "Pipit", "Job"};

Console.WriteLine("Upper Case: ");
Consumer(names, item => Console.WriteLine(item.ToUpper()));

Console.WriteLine("Length: ");
Consumer(names, item => Console.WriteLine(item.Length));
}
}
}

Capture screen

Action Delegate

Action Delegate

Referensi : dari berbagai sumber.

Semoga bermanfaat.

  1. July 19, 2016 at 15:10

    blog baru ane broh

  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: