是什麼在C#6初始化屬性下面的表達式之間的區別:差6
1.自動財產構造
public class Context1
{
public Context1()
{
this.Items = new List<string>();
}
public List<string> Items { get; private set; }
}
初始化
2:屬性從背場
public class Context2
{
private readonly List<string> items;
public Context2()
{
this.items = new List<string>();
}
public List<string> Items
{
get
{
return this.items;
}
}
}
初始化
3:在C#自動屬性的新語法6
public class Context3
{
public List<string> Items { get; } = new List<string>();
}
4:在C#自動屬性的新語法6
public class Context4
{
public List<string> Items => new List<string>();
}
所以編號4是像一個常數,而是隨着引用類型!? –
如果用「常量」表示「不斷返回此值的新實例」(àla https://xkcd.com/221),那麼......呃,我想是這樣的。但這不是「常量」的意思。有一個原因引用類型*不能*是常量。 – BoltClock
數字1和2是否相似? –