If I do this I get a System.StackOverflowException
:
private string abc = "";
public string Abc
{
get
{
return Abc; // Note the mistaken capitalization
}
}
I understand why -- the property is referencing itself, leading to an infinite loop. (See previous questions here and here).
What I'm wondering (and what I didn't see answered in those previous questions) is why doesn't the C# compiler catch this mistake? It checks for some other kinds of circular reference (classes inheriting from themselves, etc.), right? Is it just that this mistake wasn't common enough to be worth checking for? Or is there some situation I'm not thinking of, when you'd want a property to actually reference itself in this way?
question from:
https://stackoverflow.com/questions/2336847/why-doesnt-the-c-sharp-compiler-stop-properties-from-referring-to-themselves 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…