C # interface

Source: Internet
Author: User

An interface describes a set of related functions that are described by a class or struct.

So a class or struct that inherits this interface must implement all the members defined in the interface.

Interfaces are declared using the interface keyword, and members can be properties, methods, indexes, and events.

Features of the interface:

1), interfaces similar to abstract classes, cannot be instantiated, any non-abstract class that implements an interface must implement all members of the interface:

When implementing a member is displayed, the implementing member cannot be accessed through the object of the class, but only through the interface instance.

When implementing a member implicitly, it can be accessed through an instance of the class and interface, but the implementing member must be public.

2), the interface cannot contain fields, constants, constructors, destructors, static members, and so on.

3), the interface member is public and does not require any access modifiers.

4), interfaces, classes, and structs can inherit multiple interfaces.

1 InterfaceIprogram2     {3         voidFun ();4     }5 6     classProgram:iprogram7     {8         //Display Interface Implementation9         voidIprogram.fun ()Ten         { OneConsole.WriteLine ("I am fun."); A         } -         Static voidMain (string[] args) -         { theIprogram i =NewProgram ();//declares an interface, but not an interface instantiation - I.fun (); - Console.readkey (); -         } +}

The difference between an implicit interface and a display interface implementation:

An implicit interface is typically used when classes and structs implement a single interface.

The display interface is required when you want to implement multiple interfaces, and the interface contains a function with the same name. And the implicit interface of the display interface is used to fail.

1  InterfaceIprogram2     {3         voidFun ();4     }5     Interfaceiaprograme{6      voidFun ();7     }8     classProgram:iprogram,iaprograme9     {Ten         //Display Interface Implementation One         voidIprogram.fun () A         { -Console.WriteLine ("I am iprogram fun."); -         } the          voidIaprograme.fun () { -Console.WriteLine ("I am iaprograme fun."); -         }  -         //implementing an Implicit interface +          //void Fun () { -            +          //} A         Static voidMain (string[] args) at         { -Program P =NewProgram (); - ((Iprogram) p). Fun (); - ((Iaprograme) p). Fun (); - Console.readkey (); -         } in}

Inheritance of interfaces:

The inheritance of an interface refers to all members of the inherited parent interface, but it is still not implemented. and allow multiple inheritance.

1 InterfaceIprogram2     {3         voidFun ();4     }5     InterfaceIaprograme:iprogram6     {7 8     }9     classProgram:iprogram, IaprogrameTen     { One         //Display Interface Implementation A         voidIprogram.fun () -         { -Console.WriteLine ("I am iprogram fun."); the         } -         //void Iaprograme.fun () -         //{ -         //Console.WriteLine ("I Am iaprograme fun."); +         //} -         //implementing an Implicit interface +         //void Fun () { A  at         //} -         Static voidMain (string[] args) -         { -Program P =NewProgram (); -             //((Iprogram) p). Fun (); - ((Iaprograme) p). Fun (); in  - Console.readkey (); to         } +}

Overlay of the interface:

1  InterfaceIprogram2     {3         voidFun ();4     }5     Abstract classIaprogram:iprogram6     {7         Abstract  Public voidAfun ();8         voidIprogram.fun ()9         {Ten Afun (); One         } A     } -     classProgram:iaprogram -     { the       -          Public Override voidAfun () -         { -Console.WriteLine ("I am aprogram fun."); +         } -         Static voidMain (string[] args) +         { AIprogram p =NewProgram (); at             //((Iprogram) p). Fun (); - P.fun (); -  - Console.readkey (); -         } -}

Implement the interface first, and then call the abstract function.

the difference between an interface and an abstract class.
Interfaces are used for specifications, and abstract classes are used for commonalities.
Only methods, properties, events, indexers can be declared in an interface. In the abstract class, you can have a method's manifest , or you can define a non-static class variable.
Abstract classes are classes, so they can only be inherited, but interfaces are implemented more than once.
Abstract classes can provide partial implementations of certain methods, and interfaces are not available.
An instance of an abstract class is given by its subclasses. An instance of an interface is given by the class that implements the interface.
By adding a method to an abstract class, its subclasses have this method at the same time. Instead of adding a new method to the interface, the class that implements it is rewritten (which is why the interface is a specification of a class).
Interface members are defined as public, but members of an abstract class can also be private , protected, internal, or protected internal members (where protected internal members can only be accessed in the application's code or derived classes).
Additionally, the interface cannot contain fields, constructors, destructors, static members, or constants.
What are the similarities and differences between interfaces and classes in C #.
Interfaces cannot be instantiated directly.
An interface does not contain an implementation of a method.
An interface can implement multiple inheritance, whereas a class can only be single-inheritance.
A class definition can be split between different source files.
interfaces, classes, and structs can inherit from multiple interfaces.
An interface is similar to an abstract base class: Any non-abstract type that inherits an interface must implement all members of the interface.
An interface can contain events, indexers, methods, and properties.
A class can implement multiple interfaces.

C # interface

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.