65.9K
CodeProject is changing. Read more.
Home

Interface & Property

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.25/5 (8 votes)

Oct 26, 2010

CPOL
viewsIcon

17069

Shows How can you keep property field as part of interface & how it will be implemented

This Tip 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 implementor 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; }
    }
}