this program is given in the page 119 , C#4 Wrox Professional C#
snippet:_
Code:
public class Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override string ToString()
{
return String.Format("Width: {0}, Height: {1}", Width, Height);
}
}
public class Rectangle: Shape
{
}
public interface IIndex < out T >
{
T this[int index] { get; }
int Count { get; }
}
public class RectangleCollection: IIndex<Rectangle>
{
private Rectangle[] data = new Rectangle[3]
{
new Rectangle { Height=2, Width=5},
new Rectangle { Height=3, Width=7},
new Rectangle { Height=4.5, Width=2.9}
};
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 // *******************************n.b
{
get
{
return data.Length;
}
}
}
class MPEP
{
static void Main()
{
IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles();
IIndex<Shape> shapes = rectangles;
for (int i = 0; i < shapes.Count; i++)
{
Console.WriteLine(shapes[i]);
}
}
}
My difficulty is :
Can an auto-implemented property contain only the get part --- I mean the set part in these cases are mandatory isn't it ?