1

这两者有什么区别,什么是“正确”的?

public interface IMessage
{
    /// <summary>
    /// Array used to hold all bytes that will be written.
    /// </summary>
    IList Buffer { get; set; }
}

public interface IMessage<T> where T : IList
{
    /// <summary>
    /// Array used to hold all bytes that will be written.
    /// </summary>
    T Buffer { get; set; }
}

编辑 1:已修复 - 接口上不能有字段。(感谢 BoltClock)
编辑 2:固定 - 不能对接口进行封装。(感谢基思)

4

3 回答 3

5

两者在概念上都是“正确的”并且在语义上意味着几乎相同的东西(如果不介意语法错误 - 因为接口不能有字段,如果它们被定义为属性就可以了)。

泛型版本允许您返回除IList- 以外的类型,因此List可以返回 a 而不是接口类型。

于 2012-07-23T19:12:22.600 回答
0

如果我没记错的话,在第二个变体中,您可以使用 IList 中您自己的类作为 T。

于 2012-07-23T19:17:42.607 回答
0

在第一种情况下,您的缓冲区被定义为 IList 类型。它可以在运行时分配 IList 的任何实现,并且该接口实现的任何创建者都不必确切知道将使用什么实现。但是,代码在运行时无法知道使用了哪个确切的具体实现,因此在设计时将此属性称为接口的成员时,您将永远无法访问 IList 接口未显式公开的任何方法。

在第二种情况下,您正在定义一个泛型。只要这个通用定义保持“开放”(未定义 T),您仍然受限于使用 IList 的方法。但是,必须关闭此泛型,无论是接口的实现还是保持泛型打开的实现的特定实例的定义。一旦关闭泛型,就可以知道确切的、具体的缓冲区类型并且可以在其上调用不一定由 IList 定义的方法。但是,一旦关闭泛型,就不能为定义特定泛型类型的实例或类提供 IList 的不同实现以用作缓冲区。

因此,一些示例(假设您无法在接口上指定的受保护字段实际上是一个可变的公共属性):

//this class implements the non-generic interface, so buffer is an IList.
class MyMessage1: IMessage
{
   public IList buffer {get;set;}

   public MyMessage1()
   {
      buffer = new List<string>();

      //even though you "know" what you just assigned, 
      //you cannot refer to buffer as a List<string>, even here.
      buffer.Sort(); //error
   }
}

...

//The exact type of buffer cannot be known statically, 
//so only non-generic IList methods are allowed
var myMessage = new MyMessage1();
myMessage.buffer.Add("my message"); //valid; string literals are Objects
var firstLen = myMessage.buffer[0].Length; //error: indexer returns Objects.
myMessage.Sort(); //error: IList does not have a Sort() method.
firstLen = GetFirstLength(myMessage); //error: not an IMessage<List<string>>
//but, an IList is an IList no matter what, so this works.
myMessage.buffer = new List<int>(); 

...

//this class keeps the generic open so T can be any IList, determined at instantiation.
class MyMessage2<T>:IMessage<T> where T:IList
{
    public T buffer {get;set;}

    //buffer's exact type is still not known here,
    //so inside this class you are still restricted to IList members only
    public int BufferCount{get{return buffer.Count;}}

    public void SortBuffer()
    {
       buffer.Sort(); //error; no such method
    }    
}

...

//but, once you define an instance, you know exactly what buffer is
var myMessage = new MyMessage2<List<string>>();
myMessage.buffer.Add("my message");
var firstLen = myMessage.buffer[0].Length; //now we know the indexer produces strings.
myMessage.buffer.Sort(); //buffer is known to be a List<T> which has Sort()
firstLen = GetFirstLength(myMessage);

...

//and when you pass it as a parameter, you can close the generic of the interface
public string GetFirstLength(IMessage<List<string>> message) 
{
   //...so you still know what you're dealing with
   return message.buffer[0].Length;
}

...

//however, buffer is now "strongly typed" and the implementation can't change
myMessage.buffer = new List<int>(); //error; buffer is of type List<string>

...

//this class closes the generic within the declaration.
class MyMessage3:IMessage<IList<string>>
{
   //now we're closing the generic in the implementation itself,
   //so internally we know exactly what we're dealing with
   public List<string> buffer {get;set;}

   //...so this call is valid
   public void SortBuffer() { buffer.Sort(); }
}

//...and consuming code doesn't have to (get to?) specify the implementation of T
var myMessage = new MyMessage3();
//... but still knows exactly what that implementation is
myMessage.buffer.Add("my message");
var firstLen = myMessage.buffer[0].Length;
myMessage.buffer.Sort();

//and btw, MyMessage3 is still an IMessage<List<string>>
firstLen = GetFirstLength(myMessage);

//... and buffer's still a strongly-typed List<string>
myMessage.buffer = new List<int>(); //error
于 2012-07-23T19:18:32.603 回答