C # programming guide
Interface attributes (C # programming guide)You can declare attributes on the interface (C # reference. The following is an example of the interface indexer accesser: C # copy the code public interface isampleinterface {// property declaration: string name {Get; Set ;}} the accessor of the interface attribute does not have a body. Therefore, the accessors are used to indicate whether the attribute is read/write, read-only, or write-only. In this example, the interface iemployee has the read/write attribute name and the read-only attribute counter. The employee class implements the iemployee interface and uses these two attributes. The program reads the name of the new employee and the current number of the employee, and displays the employee name and the employee number calculated. You can use the fully qualified attribute name to reference the declared member interface. For example, C # copy the code string iemployee. Name {get {return "employee name" ;}set {}}, which is called explicit interface implementation (C # programming guide ). For example, if the employee class implements two interfaces, icitizen and iemployee, and both interfaces have the name attribute, explicit interface member implementation is required. That is, the following attribute Declaration: C # copy the code string iemployee. name {get {return "employee name";} set {} implements the name attribute on the iemployee interface, and the following statement: C # copy the code string icitizen. name {get {return "Citizen name";} set {}} implements the name attribute on the icitizen interface. C # copy the code interface iemployee {string name {Get; set;} int counter {Get ;}} publicclass EMPLOYEE: iemployee {publicstaticint numberofemployees; privatestring name; publicstring name // read-write instance property {get {return name;} set {name = value ;}} privateint counter; publicint counter // read-only instance property {get {return counter;} public employee () // constructor {counter =++ counter + numberofemployees ;}} class testemployee {staticvoid 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 );}}
Input210 Hazem abolrous example 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 (Source: msdn)