Interface attributes (C # programming guide)

Source: Internet
Author: User

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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.