关于C# 泛型接口的代码?

看c# 高级编程,书中的例子 有一段看不明白。
public Rectangle this[int index] {。。。}

全部代码:


//
public interface IIndex<out T>
{
    T this[int index] { get; }
    int Count { get; }
}




public class RectangleCollection : IIndex<Rectangle>
{
    private Rectangle[] data = new Rectangle[3]
    {
   // ……
    };

    public static RectangleCollection GetRectangles()
    {
         return new RectangleCollection();
    }

    **public Rectangle this[int index]**
    {
        get
         {
            if (index < 0 || index > data.Length)
            throw new ArgumentOutOfRangeException("index");
            return data[index];
        }
    }
    public int Count
    {
        get
        {
        return data.Length;
        }
    }
}
阅读 2.2k
1 个回答

看不明白就再去看看 泛型 跟 索引器

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进