Placing property as part of Interface

This blog shows how do you keep property as part of the interface, how do you implement that property on your class.

Consider the inteface below:

interface IBus
{
    float DiscountPrice {get;}
}

Here, we specified that implementer of this interface, should implement the property DiscountPrice. Also, note that we asked for retrieval of the property value only needs to be implemented.

Below is the class that implements the above interface property:

public class SchoolBus:IBus
{
    private int Price = 8000;
   
//Implementing the property
    public float DiscountPrice
    {
        get { return price * 0.95f; }
    }
}

 

So the Interface forces to Implement the DiscountPrice Property. Also it forces that it should be read only.

Ebook Download
View all
Learn
View all