Language => Speaker
Ruby => rubyist
Python => pythonista
.NET => dotnetter
Lisp => lisper
Scheme => schemer
PHP => ???
Java => ???
Apa lagi?
Siri Bercakap Dengan C# 3.5 (02) - Automatically Implemented Property
Selamat kembali untuk siri kedua mengenali C# 3.5. Kali ini kita akan bercakap mengenai automatically implemented property.
Dalam C#, kita menggantikan penggunaan getter dan setter yang ada dalam language-language OO yang lain seperti Java dan C++ dengan Property, yang mana pengisytiharannya adalah seperti berikut:
class Student
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
--- >8 ---
var student = new Student();
student.Name = "Ikhwan";
Kebanyakan masa, variable _name tidak digunakan langsung pun secara terus. Semua operasi akan melalui property Name. Oleh itu, C# 3.5 menyediakan kemudahan yang mana ia boleh secara automatic mencipta private variable tersebut. Caranya seperti berikut:
class Student
{
public string Name { get; set; }
}
Property Name masih boleh digunakan seperti biasa.
Timbul pula keperluan dimana kadangkala kita mahukan read-only property, yang biasanya dibuat seperti berikut:
class Student
{
public string Name { get; set; }
private Guid _id;
public Guid Id
{
get { return _id; }
}
public Student(Guid id)
{
_id = id;
}
}
--- >8 ---
var student = new Student(Guid.NewGuid());
student.Name = "Ikhwan";
Untuk menggunakan automatically implemented property dalam bentuk ini, kita boleh letakkan access modifier pada klausa set.
class Student
{
public string Name { get; set; }
public Guid Id { get; private set; }
public Student(Guid id)
{
this.Id = id;
}
}
Id masih lagi read-only pada pandangan pihak luar. Cool ‘aight?
Artikel lain dalam siri ini:
Siri Bercakap Dengan C# 3.5 (01) - Type Inference
Aku guna C# 3.5 dalam development aku sekarang. C# versi baru ini ada beberapa feature yang menarik berbanding versi lamanya. Aku cuba guna sikit-sikit, mana yang aku dapat tangkap, dan aku fikir elok aku kongsikan sekali dengan korang. Moga-moga aku lebih cepat faham dan ingat.
Oleh itu, aku akan cuba jadikan entry ini sebagai entry pertama dalam satu siri yang akan menunjukkan feature-feature baru C# 3.5. Aku sebut “cuba” sebab kebelakangan ni aku agak sibuk, jadi mungkin siri ni akan lambat habis, bergantung pada kelapangan aku.
Audience tumpuan aku ialah para pengguna C# yang lama, mungkin 1.0 atau 2.0. Korang kena ada asas C# dahulu untuk mudah faham. Sebenarnya ada antara feature ini ditambah pada C# 3.0, tapi nak senang cerita, kita sebut C# 3.5 ajelah. Merujuk pada versi bahasa C# sendiri dengan gandingan .NET Framework 3.5.
Kita mula dengan feature yang mudah dulu, iaitu type inference. C# ialah strongly typed language. Bermaksud, C# mewajibkan setiap variable untuk mempunyai typenya, mungkin sama ada int, float, decimal, string, DateTime; atau type yang kita cipta sendiri melalui pengisytiharan class atau struct.
Jika dulu, bila kita ingin mengisytiharkan sesuatu variable, kita kena sertakan typenya sekali.
string ayat = "Hitam kegemaranku.";
Variable ayat adalah dari jenis string. Ia akan disimpan sebagai string di dalam memory dan akan hidup sebagai string sepanjang jangka hayatnya.
Tapi dalam C# 3.5, kita boleh gantikan type tadi dengan keyword var.
var ayat = "Hitam kegemaranku.";
Tak berapa nampak bezanya, tapi cuba kita gunakan cara sama untuk type yang lebih panjang namanya.
// C# lama My.Long.Name.Space.Domains.Student student = new My.Long.Name.Space.Domains.Student(); // C# baru var student = new My.Long.Name.Space.Domains.Student();
Menjimatkan? Sudah tentu… Sayangi jari-jemari anda.
Ini tidak bermaksud bahawa variable itu boleh mempunyai apa-apa type (loosely typed). ayat masih lagi berjenis string dan tidak berubah. Tetapi melalui type inference, compiler akan pandai-pandai meneka (infer) type variable tersebut, semasa compile-time.
Type inference juga boleh digunakan untuk pengisytiharan array.
// C# lama
Student[] students = new Student[] { new Student("Ali"), new Student("Abu") };
// C# baru
Student[] students = new[] { new Student("Ali"), new Student("Abu") };
// atau
var students = new[] { new Student("Ali"), new Student("Abu") };
Lebih pendek kan? Cantik…
Type inference sebenarnya dah ada lama dalam language-language lain seperti OCaml dan Haskell. Baguslah C# dah adopt ciri ini. Sebenarnya dalam C# 3.5, ada banyak ciri-ciri yang bagus yang ditambah, yang banyak memudahkan programmer, dan menjadikan code lebih elegant.
Real IT Dudes Call ‘Em “Machines”
When an IT guy says “machine”, you should intuitively know that he is, most of the time, referring to a computer, a PC, a server, or even a laptop. We call them “machines” ‘coz it sounds so darn freakin’ cool and macho.
i, j, dan k Dalam for Loop
Pernah terfikir kenapa selalu orang pakai variable i, j, k dalam for loop? Dah jadi macam adat pulak. Aku jarang sekali sampai j atau k, mungkin sebab aku biasa guna more advance data structure mcm hashtable dans sebagainya. Apa-apa pun, ini asal-usulnya: Why we use i, j, and k in for loops.
Find Happiness In Anything
I believe that you can find happiness anywhere, if you know where to look for. Every cloud has silver lining, that sorts of things.
In a really sad situation, like maybe you just lost you pet hamster (Hamtaro), I’m sure you can find the bright side of it. At least you don’t have to clean his cage, or buy his food, etc. That extra money can be put into savings, invest in unit trust funds, which will grow and multiply, and eventually you might get that Ferrari you’ve been dreaming for. It might not necessarily be a Ferrari, I just couldn’t think of any other materialistic luxury right now.
Or let’s say your boyfriend/girlfriend just dumps you. Maybe he/she is just not meant for you. Maybe someone better will come into your life some time soon, and may even be your future spouse. You can’t tell the future right? So don’t be too sad.
Or maybe you just lost your job. You hated it anyway, but you’ve been doing it for 20 years and you don’t know how to do anything else. Your life has been miserable right from birth. Your mother hates you, your father ran away. You’re not so bright in class, been bullied by classmates. You’re weak and hopeless, you’re just like Nobita but without the Doraemon. Your car just got stolen few months back. You got know friends, people can’t stand your smell. You’re almost 40 and you’re single. You tried to cut your wrist a few times, but you’re afraid of blood.
Well.. err… SUCKS TO BE YOU!
Just kidding, don’t take that seriously. I’m sure there’s something to be happy about…
ASP.NET 3.5 Extensions CTP
AWESOME!!! Tadi sebelum balik, Irwan dalam keadaan hampir-hampir melompat kegembiraan, memberitahu aku, ASP.NET MVC dah keluar! Eleh, relax la brader. Aku pun gembira jugak, tapi cover-cover la sikit :P
Dah lama aku tunggu, released jugak akhirnya. Ini pengumumannya dari blog ScottGu. Rupanya ia released sebagai salah satu feature dalam ASP.NET 3.5 Extensions. Quoted dari blog entry tersebut:
This first ASP.NET 3.5 Extensions preview release includes:
- ASP.NET AJAX Improvements: New ASP.NET AJAX features in the ASP.NET 3.5 Extensions release include better browser history support (back/forward button integration, and server-side history management support), improved AJAX content linking support with permalinks, and additional JavaScript library improvements.
- ASP.NET MVC: This model view controller (MVC) framework for ASP.NET provides a structured model that enables a clear separation of concerns within web applications, and makes it easier to unit test your code and support a TDD workflow. It also helps provide more control over the URLs you publish in your applications, and more control over the HTML that is emitted from them.
- ASP.NET Dynamic Data Support: The ASP.NET 3.5 Extensions release delivers new features that enable faster creation of data driven web sites. It provides a rich scaffolding framework, and will enable rapid data driven site development using both ASP.NET WebForms and ASP.NET MVC.
- ASP.NET Silverlight Support: With the ASP.NET 3.5 Extensions release we’ll deliver support for easily integrating Silverlight within your ASP.NET applications. Included will be new controls that make it easy to integrate Silverlight video/media and interactive content within your sites.
- ADO.NET Data Services: In parallel with the ASP.NET Extensions release we will also be releasing the ADO.NET Entity Framework. This provides a modeling framework that enables developers to define a conceptual model of a database schema that closely aligns to a real world view of the information. We will also be shipping a new set of data services (codename “Astoria”) that make it easy to expose REST based API endpoints from within your ASP.NET applications.
Antara perkara yang menarik perhatian perhatian aku:
MVC — Sudah tentulah! Hahha.
Scaffolding — Kalau ini scaffolding berdasarkan database (table), tak mahu lah aku. Aku mencari scaffolding dari business object.
Astoria — Web service berasaskan REST instead of SOAP? Menarik tu.
Ia hanya CTP version (Community Technology Preview), jadi memang tak stabil, tak capai tahap beta pun. Aku pun tak sempat lagi nak try buat masa ni, sibuk bangat. Tapi for sure, kitorang akan gunakan ASP.NET MVC ni utk project yang akan datang. Kalau betul scaffolding tu boleh buat berdasarkan business object, lagi bagus, boleh develop system in blinding speed.

