How do you give a C# auto-property an initial value?
(如何为C#自动属性赋予初始值?)
I either use the constructor, or revert to the old syntax.
(我要么使用构造函数,要么恢复为旧语法。)
Using the Constructor:
(使用构造函数:)
class Person
{
public Person()
{
Name = "Initial Name";
}
public string Name { get; set; }
}
Using normal property syntax (with an initial value)
(使用常规属性语法 (带有初始值))
private string name = "Initial Name";
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Is there a better way?
(有没有更好的办法?)
ask by bentford translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…