NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

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 below.

Remarks

The body of the accessor of an interface property consists of a semicolon.

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.

// 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.FromString(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);
   }
}

Sample Run

Enter number of employees: 201
Enter the name of the new employee: Vivian Nicola
The employee information:
Employee number: 202
Employee name: Vivian Nicola

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 on Explicit Interface Member Implementation, see § 13.4.1 in the language reference.

See Also

Properties | Accessors | Comparison Between Properties and Indexers