What are abstract properties in C#?

CsharpProgrammingServer Side Programming

An implementation of the property accessors will not be provided by an abstract property declaration.

Let us see how to learn how to work with abstract properties. Here we have an abstract class Shape with two derived classes: Square and Circle.

Here, we have an abstract Area property.

The following is the Circle class.

Example

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;
      }
   }
}

In the same way, the Square class.

Example

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;
      }
   }
}

The following is the Shape class, which is abstract.

Example

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);
   }
}
raja
Published on 18-Sep-2018 12:41:34
Advertisements