在 C# 接口中自动实现的属性
Auto implemented Properties in C# Interfaces
我指的是 Microsoft 的文档 - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties。他们指出,当 get 或 set 中不需要额外的逻辑时,自动实现的属性基本上是没有主体的属性。所以 int Myproperty1 {get;set;}
是自动实现的 属性。该文档还说明了以下几点
陈述1:
“您不能在接口中声明自动实现的属性。自动实现的属性声明私有实例支持字段,而接口不能声明实例字段。”
但我可以在界面中声明自动实现 属性 如下所示
public MyInterface { int Myproperty1 {get;set;}
。这与我们不能在接口中声明自动实现的属性的上述声明不冲突吗?
Microsoft 文档然后说:
声明2:
“在没有定义主体的情况下在接口中声明 属性 会声明具有访问器的 属性,访问器必须由实现该接口的每个类型实现。”
我不明白什么是声明 属性 没有 body ,它不是自动实现的吗 属性,如果是那么第一个语句是不是不正确?
对问题的重要编辑: 抱歉,我错误地发布了带有此 link 的问题:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/interface-properties。
虽然我打算参考以下 link:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties。我现在已经用正确的 link 更新了我的问题。
MSDN 从未说过“所有没有主体的属性都是自动实现的属性”之类的话。他们可能会说“自动实现的属性没有主体”,但后者并不意味着前者。 MSDN 并没有自相矛盾。
接口中没有主体的属性是 abstract
,而自动实现的属性是那些非抽象的、没有主体的,并且在 class/struct.
中
因此,public MyInterface { int MyProperty1 {get;set;} }
中的MyProperty1
不是一个自动实现的属性,而是一个抽象的。
I fail to understand what is declaring a property without body
这就像在接口中声明两个没有主体的方法:
public MyInterfaceWithTwoMethods {
int GetMyProperty1();
void SetMyProperty1(int value);
}
除了在 C# 中使用属性更为惯用。
您可以 实现 MyInterface
并自动实现 属性:
public class MyImpl : MyInterface {
public int MyProperty1 { get; set; }
}
即使您看起来只是在重复MyInterface
中写的内容,这类似于像这样实现MyInterfaceWithTwoMethods
:
public class MyImpl : MyInterfaceWithTwoMethods {
private int myProperty1;
int GetMyProperty1() => myProperty1;
void SetMyProperty1(int value) { myProperty1 = value; }
}
您还可以使用自动实现的 属性:
MyInterface
not
public class MyImpl : MyInterface {
public int MyProperty1 {
get => 1;
set { Console.WriteLine("foo"); }
}
}
我指的是 Microsoft 的文档 - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties。他们指出,当 get 或 set 中不需要额外的逻辑时,自动实现的属性基本上是没有主体的属性。所以 int Myproperty1 {get;set;}
是自动实现的 属性。该文档还说明了以下几点
陈述1: “您不能在接口中声明自动实现的属性。自动实现的属性声明私有实例支持字段,而接口不能声明实例字段。”
但我可以在界面中声明自动实现 属性 如下所示
public MyInterface { int Myproperty1 {get;set;}
。这与我们不能在接口中声明自动实现的属性的上述声明不冲突吗?
Microsoft 文档然后说: 声明2: “在没有定义主体的情况下在接口中声明 属性 会声明具有访问器的 属性,访问器必须由实现该接口的每个类型实现。”
我不明白什么是声明 属性 没有 body ,它不是自动实现的吗 属性,如果是那么第一个语句是不是不正确?
对问题的重要编辑: 抱歉,我错误地发布了带有此 link 的问题: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/interface-properties。 虽然我打算参考以下 link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties。我现在已经用正确的 link 更新了我的问题。
MSDN 从未说过“所有没有主体的属性都是自动实现的属性”之类的话。他们可能会说“自动实现的属性没有主体”,但后者并不意味着前者。 MSDN 并没有自相矛盾。
接口中没有主体的属性是 abstract
,而自动实现的属性是那些非抽象的、没有主体的,并且在 class/struct.
因此,public MyInterface { int MyProperty1 {get;set;} }
中的MyProperty1
不是一个自动实现的属性,而是一个抽象的。
I fail to understand what is declaring a property without body
这就像在接口中声明两个没有主体的方法:
public MyInterfaceWithTwoMethods {
int GetMyProperty1();
void SetMyProperty1(int value);
}
除了在 C# 中使用属性更为惯用。
您可以 实现 MyInterface
并自动实现 属性:
public class MyImpl : MyInterface {
public int MyProperty1 { get; set; }
}
即使您看起来只是在重复MyInterface
中写的内容,这类似于像这样实现MyInterfaceWithTwoMethods
:
public class MyImpl : MyInterfaceWithTwoMethods {
private int myProperty1;
int GetMyProperty1() => myProperty1;
void SetMyProperty1(int value) { myProperty1 = value; }
}
您还可以使用自动实现的 属性:
MyInterface
not
public class MyImpl : MyInterface {
public int MyProperty1 {
get => 1;
set { Console.WriteLine("foo"); }
}
}