O'Reilly logo

Microsoft® Visual C#® 2008 Step by Step by John Sharp

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Declaring Interface Properties

You encountered interfaces in Chapter 13, "Creating Interfaces and Defining Abstract Classes." Interfaces can define properties as well as methods. To do this, you specify the get or set keyword, or both, but replace the body of the get or set accessor with a semicolon. For example:

interface IScreenPosition
{
    int X { get; set; }
    int Y { get; set; }
}

Any class or structure that implements this interface must implement the X and Y properties with get and set accessor methods. For example:

struct ScreenPosition : IScreenPosition
{
    ...
    public int X
    {
        get { ... }
        set { ... }
    }

    public int Y
    {
        get { ... }
        set { ... }
    }
    ...
}

If you implement the interface properties in a class, you can declare the property implementations ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required