C # Basic Knowledge Review (10)---interface and interface inheritance

Source: Internet
Author: User

1. The interface must start with I;

2. Only declaration, not achieved;

3. The implementation of the class in the implementation method, the name must be consistent with the interface;

4. Implementation of the class in the implementation of the inheritance interface, the parent interface must be implemented in conjunction with the method;

5. Interface declaration, the default is public, this is different from class.

C#interface (Interface)

The interface defines the syntax contracts that should be followed when all classes inherit an interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "How to" section of the syntax contract.

An interface defines properties, methods, and events, which are members of an interface. The interface contains only the declarations of the members. The definition of a member is the responsibility of the derived class. The interface provides the standard structure that a derived class should follow.

Interfaces make the class or structure that implements the interface consistent in form.

Abstract classes are somewhat similar to interfaces, but most of them are only used when only a few methods are declared by the base class by a derived class.

Defining Interfaces: MyInterface.cs

An interface uses the interface keyword declaration, which is similar to a class declaration. The interface declaration is public by default. The following is an instance of an interface declaration:

Interfaceimyinterface{voidmethodtoimplement();}     

The above code defines the interface IMyInterface. Usually interface commands start with the letter I, this interface has only one method Methodtoimplement (), no parameters and return values, of course, we can install the requirements set parameters and return values.

It is important to note that this method does not have a specific implementation.

Next we will implement the above interface: InterfaceImplementer.cs
Using System;Interface IMyInterface{Interface Members void Methodtoimplement();}Class Interfaceimplementer : IMyInterface{ Static void Main() { Interfaceimplementer iImp = newinterfaceimplementer ();  Iimp. Methodtoimplement} public void  methodtoimplement ()  {< Span class= "PLN" > console. Writeline ( }}       /span>                

The Interfaceimplementer class implements the IMyInterface interface, and the implementation of the interface is similar to the class's inherited syntax format:

Classinterfaceimplementer:imyinterface  

After inheriting the interface, we need to implement the method Methodtoimplement () of the interface, and the method name must match the method name defined by the interface.

Interface Inheritance: InterfaceInheritance.cs

The following example defines two interfaces IMyInterface and Iparentinterface.

If an interface inherits other interfaces, implementing a class or struct requires implementing the members of all interfaces.

The following instance IMyInterface inherits the Iparentinterface interface, so the interface implementation class must implement the Methodtoimplement () and Parentinterfacemethod () methods:

Using System;Interface Iparentinterface{ void Parentinterfacemethod();}Interface IMyInterface : Iparentinterface{ void Methodtoimplement();}Class Interfaceimplementer : IMyInterface{ Static void Main() { InterfaceimplementerIImp= New Interfaceimplementer();IImp.Methodtoimplement();IImp.Parentinterfacemethod(); } Public void methodtoimplement ()  { Span class= "Typ" >console. Writeline ( } public  parentinterfacemethod () { console. Writeline ( }}       /span>                

The result of the instance output is:

Methodtoimplement() called.  Parentinterfacemethod() called.      

C # Basic Knowledge Review (10)---interface and interface inheritance

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.