Home > Uncategorized > 6. Java vs C# (Extension Method)

6. Java vs C# (Extension Method)

kita bisa menambakan method seolah2 method tersebut terletak di kelas tersebut.

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}

kita bisa menggunakannya sebagai berikut

using ExtensionMethods;
....
string s = "Hello Extension Methods";
int i = s.WordCount(); // seolah2 ada method WordCount pada string.
....

klo make java harus gini …

public class StringUtils
    {
        public static int WordCount(String str)
        {
              // something like this lah in java
             // return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }

manggilanya seperti ini

String s = "Hello Extension Methods";
int i = StringUtils.WordCount(s);

jelas c# lebih mudah dibaca dengan extension method…..

Categories: Uncategorized Tags: , ,
  1. Rikky Hasibuan
    February 11, 2010 at 5:15 pm

    Hhhmmm… Nice!!! ^^v

  2. weltam
    February 12, 2010 at 1:16 am

    i believe you will say that..😀

  3. February 19, 2010 at 12:26 pm

    dari postinganmu bro, kyanya dah berpindah aliran yah…

    dari aliran perguruan pencak silat SUN (yg sekarang punya si Oracle) mejadi aliran MICROSOFT…

    CMIIW

    • weltam
      February 21, 2010 at 3:03 pm

      lebih tertarik ke .net aku jadinya bro… enak keknya… zero friction development😀

  4. March 6, 2010 at 9:51 am

    Keren ya …
    Btw bang … bagi dulu Tutorialmu mengenai DDD …
    Tanggung kemarin dapat pas KP …
    😀

  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: