好奇这个自引用代码如何有效或能够编译?
public class TestClass
{
public string TestProperty = nameof(TestProperty);
}
Run Code Online (Sandbox Code Playgroud)
该nameof表达式是在程序编译期间解析的。编译器知道属性的名称TestProperty并将替换nameof(TestProperty)为"TestProperty".
nameof(TestProperty)依赖于TestProperty,但TestProperty不依赖于nameof(TestProperty)。因此,不存在循环依赖,编译器可以毫无问题地确定该值