1 static void Main(string[] args)
2 {
3 IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles();
4 IIndex<Shape> shapes = rectangles;
5 Console.ReadKey();
6 }
7
8
9 public interface IIndex<out T>
10 {
11 T this[int index] { get; }
12 int Count { get; }
13 }
14 public class RectangleCollection : IIndex<Rectangle>
15 {
16 private Rectangle[] data = new Rectangle[3]
17 {
18 new Rectangle{Width=2,Height=3},
19 new Rectangle{Width=3,Height=7},
20 new Rectangle{Width=4.5,Height=2.9}
21 };
22 private static RectangleCollection coll;
23
24 public static RectangleCollection GetRectangles()
25 {
26 return coll ?? (coll = new RectangleCollection());
27 }
28 public Rectangle this[int index]
29 {
30 get
31 {
32 if (index < 0 || index > data.Length)
33 {
34 throw new ArgumentOutOfRangeException("index");
35 }
36 return data[index];
37 }
38 }
39 public int Count
40 {
41 get
42 {
43 return data.Length;
44 }
45 }
46 }
47 public class Shape
48 {
49 public double Width { get; set; }
50 public double Height { get; set; }
51 public override string ToString()
52 {
53 return String.Format("width:{0},height:{1}", Width, Height);
54 }
55 }
56 public class Rectangle : Shape
57 {
58
59 }