标签: property-initializer

班级中的属性顺序是否重要?

我有以下课程:

class Foo
{
    public Foo()
    {
        Console.WriteLine("Foo");
    }

    public string A { get; set; } = GetStr("A");

    public string B { get; set; } = GetStr("B");

    public static string GetStr(string str)
    {
        Console.WriteLine(str);
        return str;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我从它创建一个实例时,输出是这样的:

A
B
Foo

如果我将我的属性更改为:

public string B { get; set; } = GetStr("B");

public string A { get; set; } = GetStr("A");
Run Code Online (Sandbox Code Playgroud)

输出是:

B
A
Foo

我的问题是:

班级中的属性顺序是否重要,可能会影响我的程序?

注意:我使用C#6.0新功能:属性初始化器更多

c# properties property-initializer

0
推荐指数
1
解决办法
245
查看次数

标签 统计

c# ×1

properties ×1

property-initializer ×1