为什么这个使用 nameof() 的自引用代码有效?

mea*_*axe 1 c#

好奇这个自引用代码如何有效或能够编译?

public class TestClass
{
  public string TestProperty = nameof(TestProperty);
}
Run Code Online (Sandbox Code Playgroud)

Man*_*uel 6

nameof表达式是在程序编译期间解析的。编译器知道属性的名称TestProperty并将替换nameof(TestProperty)"TestProperty".

nameof(TestProperty)依赖于TestProperty,但TestProperty不依赖于nameof(TestProperty)。因此,不存在循环依赖,编译器可以毫无问题地确定该值