MSDN never said anything like "all properties without bodies are auto-implemented properties". They might say "auto-implemented properties don't have bodies", but the latter doesn't imply the former. MSDN is not contradicting itself.
Properties without bodies in an interface are abstract
, whereas auto-implemented properties are those that are non-abstract, without bodies, and in a class/struct.
Therefore, MyProperty1
in public MyInterface { int MyProperty1 {get;set;} }
is not an auto-implemented property, but an abstract one.
I fail to understand what is declaring a property without body
It's just like declaring two methods without bodies in an interface:
public MyInterfaceWithTwoMethods {
int GetMyProperty1();
void SetMyProperty1(int value);
}
Except it's more idiomatic to use properties in C#.
You could implement MyInterface
with an auto-implemented property:
public class MyImpl : MyInterface {
public int MyProperty1 { get; set; }
}
Even though you seem to be just repeating what is written in MyInterface
, this is analogous to implementing MyInterfaceWithTwoMethods
like this:
public class MyImpl : MyInterfaceWithTwoMethods {
private int myProperty1;
int GetMyProperty1() => myProperty1;
void SetMyProperty1(int value) { myProperty1 = value; }
}
You could also implement MyInterface
not with an auto-implemented property:
public class MyImpl : MyInterface {
public int MyProperty1 {
get => 1;
set { Console.WriteLine("foo"); }
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…