C# Program to Demonstrate Abstract Properties

This is a C# Program to demonstrate abstract properties.

Problem Description

This C# Program Demonstrates Abstract Properties.

Problem Solution

Here An abstract property declaration does not provide an implementation of the property accessors.

Program/Source Code

Here is source code of the C# Program to Demonstrate Abstract Properties. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  CC# Program to Demonstrate Abstract Properties
 */
using System;
 
public abstract class Shape
{
    private string myId;
 
    public Shape(string s)
    {
        Id = s;  
    }
 
    public string Id
    {
        get
        {
            return myId;
        }
 
        set
        {
            myId = value;
        }
    }
    public abstract double Area
    {
        get;
    }
 
    public override string ToString()
    {
        return Id + " Area = " + string.Format("{0:F2}", Area);
    }
}
public class Square : Shape
{
    private int mySide;
 
    public Square(int side, string id)
        : base(id)
    {
        mySide = side;
    }
 
    public override double Area
    {
        get
        {
            // Given the side, return the area of a square:
            return mySide * mySide;
        }
    }
}
 
public class Circle : Shape
{
    private int myRadius;
 
    public Circle(int radius, string id)
        : base(id)
    {
        myRadius = radius;
    }
 
    public override double Area
    {
        get
        {
            // Given the radius, return the area of a circle:
            return myRadius * myRadius * System.Math.PI;
        }
    }
}
 
public class Rectangle : Shape
{
    private int myWidth;
    private int myHeight;
 
    public Rectangle(int width, int height, string id)
        : base(id)
    {
        myWidth = width;
        myHeight = height;
    }
 
    public override double Area
    {
        get
        {
            // Given the width and height, return the area of a rectangle:
            return myWidth * myHeight;
        }
    }
}
public class TestClass
{
    public static void Main()
    {
        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);
        }
        Console.ReadLine();
    }
}
Program Explanation

This C# Program is used to demonstrate Abstract Properties. An abstract property declaration does not provide an implementation of the property accessors. Using get and set properties in the shape class declare the variables and return the value. Compute the area of square, circle and rectangle to the called variable.

advertisement
advertisement

The foreach statement is used to iterate through the collection to get the information that we want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects.

Runtime Test Cases
 
Shapes Collection
Square #1 Area = 25.00
Circle #1 Area = 28.27
Rectangle #1 Area = 20.00

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.