logo search
CSharp_Prog_Guide

Результат 3

Enter the side: 4

Area of the square = 16.00

Area of the cube = 96.00

Enter the area: 24

Side of the square = 4.90

Side of the cube = 2.00

Interface Properties

Properties can be declared on an interface. 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. 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 { }

}