Home > Uncategorized > 2. Java vs C# (Indexers)

2. Java vs C# (Indexers)

java version dari wrapper untuk list mahasiswa.

public class MahasiwaBlackList {
   private List<Mahasiwa> _mahasiswaBlackList = new ArrayList<Mahasiwa>();
   
   public void addBadMahasiswa(Mahasiwa mahasiswa) {
      _mahasiswaBlackList.add(mahasiswa);
   }
   
   public Mahasiwa getBadMahsiswa(int rating) {
      //if (_mahasiswaBlackList.size() == 0) throw ThereNoBadStudentException();
      return _mahasiswaBlackList.get(rating);
   }
   
   public static void main(String[] args) {
      MahasiwaBlackList a = new MahasiwaBlackList();
      a.addBadMahasiswa(new Mahasiwa());
      
      System.out.println(a.getBadMahsiswa(0)); // using method
   }

di c# lebih cool dengan indexers

public class MahasiswaBlackList
    {
        // default private
        List<Mahasiswa> _mahasiswaBlackList = new List<Mahasiswa>();

        public void AddBadMahasiswa(Mahasiswa mahasiswa)
        {
            _mahasiswaBlackList.Add(mahasiswa);
        }

        public Mahasiswa this[int rating]
        {
            get { return _mahasiswaBlackList[rating]; }
        }

        static void Main()
        {
            MahasiswaBlackList blackList = new MahasiswaBlackList();
            blackList.AddBadMahasiswa(new Mahasiswa {
                GPA = 0.1,
                Name = "Longgur",
                NIM = "11111111"
            });

            Console.WriteLine(blackList[0]); // using indexers => like an array isn't ?
        }
    }

yeah.. yeah…

Categories: Uncategorized Tags: , ,
  1. February 1, 2010 at 5:50 am

    tapi liat2 dulu lah bro, masak IP nya 0.1. ah, parah nian.

    • weltam
      February 1, 2010 at 6:14 am

      hahaha… just an example man…
      😀

  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: