Nano's Blog

01/02/2010

how properties are an integral part of the C# programming language – Part 2

Filed under: programming — Tags: , — nano @ 13:40

The following example shows how to define abstract properties. An abstract property declaration does not provide an implementation of the property accessors. The example demonstrates how to override these properties in subclasses.

This sample consists of three files. In the Properties Sample, these files are compiled into a single compilation but in this tutorial, each file is compiled individually and its resulting assembly referenced by the next compilation:


// compile with: /target:library
// csc /target:library abstractshape.cs
using System;

// abstractshape.cs The Shape class that contains an abstract Area property
public abstract class Shape
{
    private string myId;

    public Shape(string s)
    {
        Id = s;   // calling the set accessor of the Id property
    }

    public string Id
    {
        get
        {
            return myId;
        }

        set
        {
            myId = value;
        }
    }

    // Area is a read-only property - only a get accessor is needed:
    public abstract double Area
    {
        get;
    }

    public override string ToString()
    {
        return Id + " Area = " + string.Format("{0:F2}", Area);
    }
}

// shapes.cs The subclasses of the Shape class.
// compile with: /target:library /reference:abstractshape.dll
// shows three subclasses of Shape and how they override the Area property to provide their own implementation.
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;
        }
    }
}

// shapetest.cs test program to display the areas of some Shape-derived objects.
// compile with: /reference:abstractshape.dll;shapes.dll
// The following code shows a test program that creates a number of Shape-derived objects and prints out their areas.
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);
        }

    }
}

- from msdn.com

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress