Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Auto implemented Properties in C# Interfaces

I am referring to the documentation of Microsoft - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties . They state that auto implemented properties are basically properties without body, when there is no additional logic needed inside a get or set. so int Myproperty1 {get;set;} is an auto implemented property. This Documentation also states below points

Statement1: "You can't declare auto-implemented properties in interfaces. Auto-implemented properties declare a private instance backing field, and interfaces may not declare instance fields."

But i can declare auto implemented property like below in an interface

public MyInterface { int Myproperty1 {get;set;} . Is this not conflicting above statement that we cant declare auto implemented properties in Interface.

Microsoft documentation then says: statement2: "Declaring a property in an interface without defining a body declares a property with accessors that must be implemented by each type that implements that interface."

I fail to understand what is declaring a property without body , is it not auto implemented property, if it is then is the first statement not incorrect?

IMPORTANT EDIT TO THE QUESTION: I apologize, I had posted the question with this link by mistake : https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/interface-properties. While I intended to refer to the following link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties. I have updated my question with the correct link now.

question from:https://stackoverflow.com/questions/65641603/auto-implemented-properties-in-c-sharp-interfaces

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"); } 
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...