public void OutMethod() { //IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles(); //IIndex<Shape> shapes = rectangles; //因为 限定了返回为Rectangle类型 所以赋值给Shape类型没有问题(简写如下) IIndex <Shape> shapes = RectangleCollection.GetRectangles(); for (int i = 0; i < shapes.Count; ++i) { Console.WriteLine(shapes[i]); } }
public static RectangleCollection GetRectangles() { return(coll ?? (coll = new RectangleCollection())); }