c#将现有类转换为正确使用属性

cod*_*der 0 c# containers properties

我有以下课程:

class Given
{
    public string text = "";
    public List<StartCondition> start_conditions = new List<StartCondition>();
};

class StartCondition
{
    public int index = 0;
    public string device = "unknown";
    public string state = "disconnected";
    public bool isPass = false;
};
Run Code Online (Sandbox Code Playgroud)

我想将它们转换为c#属性(使用get;和set;)

看看这个问题:什么是get-set-syntax-in-c,似乎我可以像这样使一个属性变得简单易行:

class Given
{
    public string text { get; set; }
    public List<StartCondition> start_conditions { get; set; }
};

class StartCondition
{
    public int index { get; set; }
    public string device { get; set; }
    public string state { get; set; }
    public bool isPass { get; set; }
};
Run Code Online (Sandbox Code Playgroud)

但是现在我不知道应该如何添加我的初始化,因为我想要与之前相同的起始值,或者对于List容器我希望它是新的.

实现这一目标的最佳方法是什么?

小智 6

自C#6.0起包含自动属性初始值设定项的功能.语法是:

public int X { get; set; } = x; // C# 6 or higher
Run Code Online (Sandbox Code Playgroud)