舉例講授C#中主動完成的屬性。本站提示廣大學習愛好者:(舉例講授C#中主動完成的屬性)文章只能為提供參考,不一定能成為您想要的結果。以下是舉例講授C#中主動完成的屬性正文
在 C# 3.0 及更高版本,當屬性拜訪器中不須要任何其他邏輯時,主動完成的屬性會使屬性聲明加倍簡練。它們還許可客戶端代碼創立對象。當你聲明以下示例中所示的屬性時,編譯器將創立僅可以經由過程該屬性的 get 和 set 拜訪器拜訪的公用、匿名支撐字段。
以下示例演示一個簡略的類,它具有某些主動完成的屬性:
// This class is mutable. Its data can be modified from
// outside the class.
class Customer
{
// Auto-Impl Properties for trivial get and set
public double TotalPurchases { get; set; }
public string Name { get; set; }
public int CustomerID { get; set; }
// Constructor
public Customer(double purchases, string name, int ID)
{
TotalPurchases = purchases;
Name = name;
CustomerID = ID;
}
// Methods
public string GetContactInfo() {return "ContactInfo";}
public string GetTransactionHistory() {return "History";}
// .. Additional methods, events, etc.
}
class Program
{
static void Main()
{
// Intialize a new object.
Customer cust1 = new Customer ( 4987.63, "Northwind",90108 );
//Modify a property
cust1.TotalPurchases += 499.99;
}
}
在 C# 6 和更高版本中,你可以像字段一樣初始化主動完成屬性:
public string FirstName { get; set; } = "Jane";
上一示例中所示的類是可變的。創立客戶端代碼後可以用於更改對象中的值。在包括主要行動(辦法)和數據的龐雜類中,平日有需要具有公共屬性。然則,關於較小類或僅封裝一組值(數據)且只要很少行動或沒有行動的構造,則應當經由過程聲明 set 拜訪器為 公用(對應用者的弗成變)或經由過程聲明僅一個 get 拜訪器 (除結構函數外都弗成變),使對象弗成變。
動完成的屬性上許可應用特征,但很顯著支撐字段上不許可,由於不克不及從你的源代碼拜訪它們。假如必需應用屬性的支撐字段上的特征,只需創立一個慣例屬性。
應用主動完成的屬性完成輕量類
本示例演示若何創立一個僅用於封裝一組主動完成的屬性的弗成變輕型類。 當你必需應用援用類型語義時,請應用此種結構而不是構造。
可經由過程兩種辦法來完成弗成變的屬性。 可以將 set 取值函數聲明為 private。 屬性只能在該類型中設置,但它關於應用者是弗成變的。 也能夠僅聲明 get 取值函數,使屬性除能在該類型的結構函數中設置,在其他任何地位都弗成變。
當你聲明一個 private set 取值函數時,你沒法應用對象初始值設定項來初始化屬性。 你必需應用結構函數或工場辦法。
示例
上面的示例演示了完成具有主動完成屬性的弗成變類的兩種辦法。 這兩種辦法均應用 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 properties.
public string Name { get; }
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-only properties.
public string Name { get; private set; }
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";
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
輸入:
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.
編譯器為每一個主動完成的屬性創立了支撐字段。 這些字段沒法直接從源代碼停止拜訪。