C# 接口中的只读和只读自动属性
我读到过,自动实现的属性不能是只读或只读的。它们只能读写 然而,在学习界面时,我遇到了福勒。代码,它创建自动属性的只读/只读和读写类型。可以接受吗C# 接口中的只读和只读自动属性,c#,properties,interface,C#,Properties,Interface,我读到过,自动实现的属性不能是只读或只读的。它们只能读写 然而,在学习界面时,我遇到了福勒。代码,它创建自动属性的只读/只读和读写类型。可以接受吗 public interface IPointy { // A read-write property in an interface would look like: // retType PropName { get; set; } // while a write-only property
public interface IPointy
{
// A read-write property in an interface would look like:
// retType PropName { get; set; }
// while a write-only property in an interface would be:
// retType PropName { set; }
byte Points { get; }
}
这不是自动实现的。接口不包含实现 它声明接口
IPointy
需要一个类型为byte
、名为Points
、具有公共getter的属性
只要有公共getter,就可以以任何必要的方式实现接口;是否通过自动属性:
public class Foo: IPointy
{
public byte Points {get; set;}
}
注意:setter仍然可以是私有的:
public class Bar: IPointy
{
public byte Points {get; private set;}
}
或者,您可以显式地编写一个getter:
public class Baz: IPointy
{
private byte _points;
public byte Points
{
get { return _points; }
}
}