如何使用VS2008中的"短样式"属性设置默认值(自动属性)?

Mat*_*ías 7 .net c# properties visual-studio-2008

如何为定义如下的属性设置默认值:

public int MyProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)

那就是在VS2008(代码片段)中使用"prop"[tab] [tab].

是否有可能不退回"旧方式"?:

private int myProperty = 0; // default value
public int MyProperty
{
    get { return myProperty; }
    set { myProperty = value; }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的时间.最好的祝福.

Chr*_*ann 9

只需在构造函数中设置"默认"值即可.

public class Person
{
   public Person()
   {
       this.FirstName = string.Empty;
   }

   public string FirstName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

此外,它们被称为自动属性.