这两者之间有什么区别,什么是"正确的"?
public interface IMessage
{
/// <summary>
/// Array used to hold all bytes that will be written.
/// </summary>
IList Buffer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public interface IMessage<T> where T : IList
{
/// <summary>
/// Array used to hold all bytes that will be written.
/// </summary>
T Buffer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑1:已修复 - 接口上不能包含字段.(感谢BoltClock)
编辑2:固定 - 无法在接口上进行封装.(谢谢KeithS)
两者在概念上都是"正确的",并且在语义上意味着几乎相同的事情(如果一个人不介意语法错误 - 因为接口不能有字段,如果它们被定义为属性,这将是好的).
通用版本允许您返回除IList- 之外的类型- 因此List可以返回而不是接口类型.