How to define abstract properties - C# (original) (raw)
The following example shows how to define abstract properties. An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.
This sample consists of three files, each of which is compiled individually and its resulting assembly is referenced by the next compilation:
- abstractshape.cs: the
Shape
class that contains an abstractArea
property. - shapes.cs: The subclasses of the
Shape
class. - shapetest.cs: A test program to display the areas of some
Shape
-derived objects.
To compile the example, use the following command:
csc abstractshape.cs shapes.cs shapetest.cs
This will create the executable file shapetest.exe.
Examples
This file declares the Shape
class that contains the Area
property of the type double
.
// compile with: csc -target:library abstractshape.cs
public abstract class Shape
{
public Shape(string s)
{
// calling the set accessor of the Id property.
Id = s;
}
public string Id { get; set; }
// Area is a read-only property - only a get accessor is needed:
public abstract double Area
{
get;
}
public override string ToString()
{
return $"{Id} Area = {Area:F2}";
}
}
- Modifiers on the property are placed on the property declaration itself. For example:
public abstract double Area
- When declaring an abstract property (such as
Area
in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a get accessor is available, so the property is read-only.
The following code shows three subclasses of Shape
and how they override the Area
property to provide their own implementation.
// compile with: csc -target:library -reference:abstractshape.dll shapes.cs
public class Square : Shape
{
private int _side;
public Square(int side, string id)
: base(id)
{
_side = side;
}
public override double Area
{
get
{
// Given the side, return the area of a square:
return _side * _side;
}
}
}
public class Circle : Shape
{
private int _radius;
public Circle(int radius, string id)
: base(id)
{
_radius = radius;
}
public override double Area
{
get
{
// Given the radius, return the area of a circle:
return _radius * _radius * Math.PI;
}
}
}
public class Rectangle : Shape
{
private int _width;
private int _height;
public Rectangle(int width, int height, string id)
: base(id)
{
_width = width;
_height = height;
}
public override double Area
{
get
{
// Given the width and height, return the area of a rectangle:
return _width * _height;
}
}
}
The following code shows a test program that creates a number of Shape
-derived objects and prints out their areas.
// compile with: csc -reference:abstractshape.dll;shapes.dll shapetest.cs
class TestClass
{
static void Main()
{
Shape[] shapes =
[
new Square(5, "Square #1"),
new Circle(3, "Circle #1"),
new Rectangle( 4, 5, "Rectangle #1")
];
Console.WriteLine("Shapes Collection");
foreach (Shape s in shapes)
{
Console.WriteLine(s);
}
}
}
/* Output:
Shapes Collection
Square #1 Area = 25.00
Circle #1 Area = 28.27
Rectangle #1 Area = 20.00
*/