How to define abstract properties (C# Programming Guide)
Article
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 abstract Area 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.
C#
// compile with: csc -target:library abstractshape.cspublicabstractclassShape
{
privatestring name;
publicShape(string s)
{
// calling the set accessor of the Id property.
Id = s;
}
publicstring Id
{
get
{
return name;
}
set
{
name = value;
}
}
// Area is a read-only property - only a get accessor is needed:publicabstractdouble Area
{
get;
}
publicoverridestringToString()
{
return$"{Id} Area = {Area:F2}";
}
}
Modifiers on the property are placed on the property declaration itself. For example:
C#
publicabstractdouble 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.
C#
// compile with: csc -target:library -reference:abstractshape.dll shapes.cspublicclassSquare : Shape
{
privateint side;
publicSquare(int side, string id)
: base(id)
{
this.side = side;
}
publicoverridedouble Area
{
get
{
// Given the side, return the area of a square:return side * side;
}
}
}
publicclassCircle : Shape
{
privateint radius;
publicCircle(int radius, string id)
: base(id)
{
this.radius = radius;
}
publicoverridedouble Area
{
get
{
// Given the radius, return the area of a circle:return radius * radius * System.Math.PI;
}
}
}
publicclassRectangle : Shape
{
privateint width;
privateint height;
publicRectangle(int width, int height, string id)
: base(id)
{
this.width = width;
this.height = height;
}
publicoverridedouble 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.
C#
// compile with: csc -reference:abstractshape.dll;shapes.dll shapetest.csclassTestClass
{
staticvoidMain()
{
Shape[] shapes =
{
new Square(5, "Square #1"),
new Circle(3, "Circle #1"),
new Rectangle( 4, 5, "Rectangle #1")
};
System.Console.WriteLine("Shapes Collection");
foreach (Shape s in shapes)
{
System.Console.WriteLine(s);
}
}
}
/* Output:
Shapes Collection
Square #1 Area = 25.00
Circle #1 Area = 28.27
Rectangle #1 Area = 20.00
*/
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Learn how to implement read-write, read-only, and write-only class properties using property accessors and access modifiers, and how to implement methods and extension methods for a class.