Home > C# and dot NET > Extension Method di C#

Extension Method di C#

February 25, 2015 Leave a comment Go to comments

Salah satu feature di C# 3.0 adalah extension method. Dengan feature tersebut dapat menambahkan fungsionalitas baru pada instance/object type. Extension method merupakan static method yang berada pada static class dengan keyword this sebagai parameter input pada method tersebut. Pada Visual Studio IntelliSense, extension method mempunyai symbol panah/arrow kebawah.

Example.

Plant.cs ( Plant class )

class Plant
{
public Plant()
{
}
}

PlantExtension.cs ( PlantExtension class )

static class PlantExtension
{
static public void PlantName(this Plant plant, string plantName)
{
Console.WriteLine("Plant name is: " + plantName);
}
}

Main class

class Program
{
static void Main(string[] args)
{
Plant plant = new Plant();
plant.PlantName("Pinus");
}
}

Capture screen.

Extension Method

Extension Method

Referensi : dari berbagai sumber.

Semoga bermanfaat.šŸ™‚

  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: