设置C#中可变的属性的默认值

pbo*_*aux 0 c#

我有一个房产

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

我的数据库中的默认值为1.如果没有另外指定,我希望此属性默认为1

public partial class test
{
    public int Id { get; set; }
    public string test1 { get; set; }
    public int active { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我在c#6中看到了你能做到的

public int active { get; set; } = 1
Run Code Online (Sandbox Code Playgroud)

但我没有使用c#6 :(.感谢您的建议.(非常非常新的c#/ OOP)

Jon*_*eet 5

只需在构造函数中设置它:

public partial class Test
{
    public int Id { get; set; }
    public string Test1 { get; set; }
    public int Active { get; set; }

    public Test()
    {
        Active = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这比仅仅为了默认而避免自动实现的属性更简单...