下面演示如何创建一个不可变的轻型类,该类仅用于封装一组自动实现的属性。 当你必须使用引用类型语义时,请使用此种构造而不是结构。
可通过以下方法来实现不可变的属性:
- 仅声明 get 访问器,使属性除了能在该类型的构造函数中可变,在其他任何位置都不可变。
- 声明 init 访问器而不是 set 访问器,这使属性只能在构造函数中进行设置,或者通过使用对象初始值设定项设置。
- 将 set 访问器声明为专用。 属性可在该类型中设置,但它对于使用者是不可变的。
可以将 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;
}
示例
以下示例演示了实现具有自动实现属性的不可变类的两种方法。 这两种方法均使用 private set 声明其中一个属性,使用单独的 get 声明另一个属性。 第一个类仅使用构造函数来初始化属性,第二个类则使用可调用构造函数的静态工厂方法。
// 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 AveCesar Garcia, 12 108th St.Debra Garcia, 89 E. 42nd St.
*/