C# 3.0 Automatic Properties

新增了一個語法,要宣告Property,只要寫下面這樣

public string FirstName  { get; set; }

Compiler就會自動幫你產生類似下面的code

string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

但一定要寫 { get; set; },也就是沒辦法只寫get來產生readonly的Property,第一次看到覺得很奇怪,那這樣我就直接宣告成Field "public string FirstName;" 就好啦~反正也沒有甚麼存取規格,今天看到的說法是 Property 在 Data Bind時跟 Field的行為不一樣。

PS: 補充,若要 readonly要改為 public string FirstName  { get; private set; }

This entry was posted in 電腦和網際網路. Bookmark the permalink.

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