Bagikan melalui


Interface Properties (C# Programming Guide)

Properties can be declared on an interface (C# Reference). The following is an example of an interface indexer accessor:

public interface ISampleInterface
{
    // Property declaration: 
    string Name
    {
        get;
        set;
    }
}

The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Example

In this example, the interface IEmployee has a read-write property, Name, and a read-only property, Counter. The class Employee implements the IEmployee interface and uses these two properties. The program reads the name of a new employee and the current number of employees and displays the employee name and the computed employee number.

You could use the fully qualified name of the property, which references the interface in which the member is declared. For example:

string IEmployee.Name
{
    get { return "Employee Name"; }
    set { }
}

This is called Explicit Interface Implementation (C# Programming Guide). For example, if the class Employee is implementing two interfaces ICitizen and IEmployee and both interfaces have the Name property, the explicit interface member implementation will be necessary. That is, the following property declaration:

string IEmployee.Name
{
    get { return "Employee Name"; }
    set { }
}

implements the Name property on the IEmployee interface, while the following declaration:

string ICitizen.Name
{
    get { return "Citizen Name"; }
    set { }
}

implements the Name property on the ICitizen interface.

interface IEmployee
{
    string Name
    {
        get;
        set;
    }

    int Counter
    {
        get;
    }
}

public class Employee : IEmployee
{
    public static int numberOfEmployees;

    private string name;
    public string Name  // read-write instance property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    private int counter;
    public int Counter  // read-only instance property
    {
        get
        {
            return counter;
        }
    }

    public Employee()  // constructor
    {
        counter = ++counter + numberOfEmployees;
    }
}

class TestEmployee
{
    static void Main()
    {
        System.Console.Write("Enter number of employees: ");
        Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());

        Employee e1 = new Employee();
        System.Console.Write("Enter the name of the new employee: ");
        e1.Name = System.Console.ReadLine();

        System.Console.WriteLine("The employee information:");
        System.Console.WriteLine("Employee number: {0}", e1.Counter);
        System.Console.WriteLine("Employee name: {0}", e1.Name);
    }
}

210 Hazem Abolrous

Sample Output

Enter number of employees: 210

Enter the name of the new employee: Hazem Abolrous

The employee information:

Employee number: 211

Employee name: Hazem Abolrous

See Also

Concepts

C# Programming Guide

Reference

Properties (C# Programming Guide)

Using Properties (C# Programming Guide)

Comparison Between Properties and Indexers (C# Programming Guide)

Indexers (C# Programming Guide)

Interfaces (C# Programming Guide)