A Property in an Interface May Include One or Two Accessors

Defining a property in an interface, you can define one or both of the property’s accessors.
1
2
3
4
5
6
public interface IDogInfo
{
    int CollarSize { get; set; }
    string Name { get; }
    string SecretName { set; }
}
When you implement an interface, if a property in the interface has both accessors, you must implement both accessors, and both must be public (you set an access modifier of public on the entire property).
If a property in the interface includes only one accessor, you must implement that accessor, but you can optionally also implement the other accessor.


1
2
3
4
5
6
7
8
9
10
11
12
13
public class Dog : IDogInfo
{
    public int CollarSize { get; set; }
 
    // IDogInfo has just get
    public string Name
    {
        get { return "NotImpl"; }
    }
 
    // IDogInfo has just set, but we add a get
    public string SecretName { protected get; set; }
}