c# – 如何使用VS2008中的“短样式”属性设置默认值(自动属性)?
发布时间:2020-12-15 08:07:39 所属栏目:百科 来源:网络整理
导读:如何为定义如下的属性设置默认值: public int MyProperty { get; set; } 那是在VS2008(代码片段)中使用“prop”[tab] [tab]. 是否有可能不退回“旧方式”?: private int myProperty = 0; // default valuepublic int MyProperty{ get { return myProperty
如何为定义如下的属性设置默认值:
public int MyProperty { get; set; } 那是在VS2008(代码片段)中使用“prop”[tab] [tab]. 是否有可能不退回“旧方式”?: private int myProperty = 0; // default value public int MyProperty { get { return myProperty; } set { myProperty = value; } } 谢谢你的时间. 解决方法
只需在构造函数中设置“默认”值即可.
public class Person { public Person() { this.FirstName = string.Empty; } public string FirstName { get; set; } } 此外,它们被称为自动属性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |