如何使用自動實作的屬性來實作輕量型類別
此範例示範如何建立不可變的輕量型類別,只封裝一組自動實作的屬性。 當您必須使用參考類型語意時,請使用這種建構,而不是結構。
您可以使用下列方式來建立不可變的屬性:
- 只宣告 get 存取子,讓屬性在該類型的建構函式外的任何位置皆為不可變的。
- 宣告 init 存取子,而不是
set
存取子,讓屬性只能在建構函式中設定,或使用物件初始設定式。 - 將 set 存取子宣告為 private。 屬性可在類型內設定,但是它對於取用者是不可變的。
您可以將 required
修飾詞加到屬性宣告,以強制呼叫者將屬性設為初始化新物件的一部分。
下列範例示範只有 get 存取子的屬性與具有 get 和 private set 的屬性有何不同。
class Contact
{
public string Name { get; }
public string Address { get; private set; }
public Contact(string contactName, string contactAddress)
{
// Both properties are accessible in the constructor.
Name = contactName;
Address = contactAddress;
}
// Name isn't assignable here. This will generate a compile error.
//public void ChangeName(string newName) => Name = newName;
// Address is assignable here.
public void ChangeAddress(string newAddress) => Address = newAddress;
}
範例
下列範例示範兩種方式可實作自動實作屬性的不可變類別。 每一種方法會宣告具有私用 set
的其中一個屬性,以及僅具有 get
的其中一個屬性。 第一個類別僅使用建構函式來初始化屬性,第二個類別使用會呼叫建構函式的靜態 Factory 方法。
// This class is immutable. After an object is created,
// it cannot be modified from outside the class. It uses a
// constructor to initialize its properties.
class Contact
{
// Read-only property.
public string Name { get; }
// Read-write property with a private set accessor.
public string Address { get; private set; }
// Public constructor.
public Contact(string contactName, string contactAddress)
{
Name = contactName;
Address = contactAddress;
}
}
// This class is immutable. After an object is created,
// it cannot be modified from outside the class. It uses a
// static method and private constructor to initialize its properties.
public class Contact2
{
// Read-write property with a private set accessor.
public string Name { get; private set; }
// Read-only property.
public string Address { get; }
// Private constructor.
private Contact2(string contactName, string contactAddress)
{
Name = contactName;
Address = contactAddress;
}
// Public factory method.
public static Contact2 CreateContact(string name, string address)
{
return new Contact2(name, address);
}
}
public class Program
{
static void Main()
{
// Some simple data sources.
string[] names = ["Terry Adams","Fadi Fakhouri", "Hanying Feng",
"Cesar Garcia", "Debra Garcia"];
string[] addresses = ["123 Main St.", "345 Cypress Ave.", "678 1st Ave",
"12 108th St.", "89 E. 42nd St."];
// Simple query to demonstrate object creation in select clause.
// Create Contact objects by using a constructor.
var query1 = from i in Enumerable.Range(0, 5)
select new Contact(names[i], addresses[i]);
// List elements cannot be modified by client code.
var list = query1.ToList();
foreach (var contact in list)
{
Console.WriteLine("{0}, {1}", contact.Name, contact.Address);
}
// Create Contact2 objects by using a static factory method.
var query2 = from i in Enumerable.Range(0, 5)
select Contact2.CreateContact(names[i], addresses[i]);
// Console output is identical to query1.
var list2 = query2.ToList();
// List elements cannot be modified by client code.
// CS0272:
// list2[0].Name = "Eugene Zabokritski";
}
}
/* Output:
Terry Adams, 123 Main St.
Fadi Fakhouri, 345 Cypress Ave.
Hanying Feng, 678 1st Ave
Cesar Garcia, 12 108th St.
Debra Garcia, 89 E. 42nd St.
*/
編譯程式會為每個自動實作的屬性建立支援欄位。 欄位無法直接從原始程式碼存取。