Export (0) Print
Expand All

Interface Properties

Properties can be declared on interfaces. The declaration takes the following form:

[attributes] [new] type identifier {interface-accessors}

where:

attributes (Optional)
Same as properties on classes.
type
Same as properties on classes.
identifier
The property name.
interface-accessors
The property accessors. See the Remarks section below.

Remarks

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.

The following is an example of an interface indexer accessor:

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

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.

// cs_interface_properties.cs
// Interface Properties
using System;
interface IEmployee 
{
   string Name 
   {
      get;
      set;
   }

   int Counter 
   {
      get;
   }
}

public class Employee: IEmployee 
{
   public static int numberOfEmployees;
   private int counter;
   private string name;
   // Read-write instance property:
   public string Name 
   {
      get 
      {
         return name; 
      }
      set 
      {
         name = value; 
      }
   }
   // Read-only instance property:
   public int Counter 
   {
      get 
      {
         return counter; 
      }
   }
   // Constructor:
   public Employee() 
   {
      counter = ++counter + numberOfEmployees;
   }
}

public class MainClass 
{
   public static void Main() 
   {
      Console.Write("Enter number of employees: ");
      string s = Console.ReadLine();
      Employee.numberOfEmployees = int.Parse(s);
      Employee e1 = new Employee();
      Console.Write("Enter the name of the new employee: ");
      e1.Name = Console.ReadLine();  
      Console.WriteLine("The employee information:");
      Console.WriteLine("Employee number: {0}", e1.Counter);
      Console.WriteLine("Employee name: {0}", e1.Name);
   }
}

Input

210
Hazem Abolrous

Sample Output

Enter number of employees: 201
Enter the name of the new employee: Hazem Abolrous
The employee information:
Employee number: 202
Employee name: Hazem Abolrous

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

public string IEmployee.Name
{
}

However, the fully qualified name is only needed to avoid ambiguity when the class is implementing more than one interface with the same property signature. 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:

public string IEmployee.Name 
{
}

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

public string ICitizen.Name 
{
}

implements the Name property on the ICitizen interface.

For more information, see 13.4.1 Explicit interface member implementations.

See Also

Properties | Accessors | Comparison Between Properties and Indexers

Show:
© 2014 Microsoft