Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are abstract properties in C#?
An abstract property in C# is a property declared in an abstract class without implementation. The derived classes must provide the actual implementation of the property accessor (get, set, or both). Abstract properties enforce a contract that ensures all derived classes implement the required property.
Abstract properties are useful when you want to define a common structure across related classes but need each class to calculate or store the property value differently.
Syntax
Following is the syntax for declaring an abstract property −
public abstract class ClassName {
public abstract DataType PropertyName {
get;
set; // optional
}
}
The derived class must override the abstract property −
public class DerivedClass : ClassName {
public override DataType PropertyName {
get {
// implementation
}
set {
// implementation (if declared in abstract class)
}
}
}
Using Abstract Properties with Shape Classes
Let's implement an abstract Area property in a Shape class that derived classes must implement −
Abstract Shape Class
using System;
public abstract class Shape {
private string name;
public Shape(string s) {
Id = s;
}
public string Id {
get {
return name;
}
set {
name = value;
}
}
public abstract double Area {
get;
}
public override string ToString() {
return Id + " Area = " + string.Format("{0:F2}", Area);
}
}
public class Circle : Shape {
private int radius;
public Circle(int radius, string id) : base(id) {
this.radius = radius;
}
public override double Area {
get {
return radius * radius * System.Math.PI;
}
}
}
public class Square : Shape {
private int side;
public Square(int side, string id) : base(id) {
this.side = side;
}
public override double Area {
get {
return side * side;
}
}
}
public class Program {
public static void Main() {
Shape circle = new Circle(5, "Circle1");
Shape square = new Square(4, "Square1");
Console.WriteLine(circle.ToString());
Console.WriteLine(square.ToString());
}
}
The output of the above code is −
Circle1 Area = 78.54 Square1 Area = 16.00
Abstract Property with Both Get and Set
Abstract properties can also include both get and set accessors −
using System;
public abstract class Vehicle {
public abstract string Model { get; set; }
public abstract void StartEngine();
}
public class Car : Vehicle {
private string model;
public override string Model {
get { return model; }
set { model = value; }
}
public override void StartEngine() {
Console.WriteLine($"{Model} engine started!");
}
}
public class Program {
public static void Main() {
Vehicle car = new Car();
car.Model = "Toyota Camry";
car.StartEngine();
Console.WriteLine("Model: " + car.Model);
}
}
The output of the above code is −
Toyota Camry engine started! Model: Toyota Camry
Key Rules
-
Abstract properties can only be declared in abstract classes.
-
Derived classes must override all abstract properties using the
overridekeyword. -
Abstract properties cannot have implementation in the abstract class.
-
You can specify
get,set, or both accessors in an abstract property.
Conclusion
Abstract properties in C# provide a way to enforce that derived classes implement specific properties while allowing each class to define its own logic for calculating or storing the property value. They are essential for creating flexible inheritance hierarchies where common properties must exist but behave differently across implementations.
