C # interface

Source: Internet
Author: User

1, in programming, we often use the interface, then what is the interface?
An interface describes a set of related functions that can belong to any class or struct, so the class or struct that implements the interface must implement the interface members specified in the interface definition.
Interfaces are defined using the interface keyword , which can consist of methods, properties, events, indexers , or any combination of these four member types .
2, the characteristics of the interface:

  1. < Span id= "xn31_8673578608a48b844b8e9a4dc82c9074" class= "sentence" > interfaces are similar to abstract base classes, cannot instantiate interface directly; interface are abstract methods, and any non-abstract type that implements the interface must implement all members of the interface: when when you explicitly implement a member of this interface, the implemented members cannot be accessed through the class instance, but only through the interface instance. When implicitly implements a member of the interface, the implemented members can be accessed through the class instance or through the interface instance, but the implemented member must be public .
    /span>
  2. < Span id= "xn31_8673578608a48b844b8e9a4dc82c9074" class= "sentence" > Interfaces cannot contain constants, fields, operators, instance constructors, destructors or types, cannot contain static members /span>
  3. interface member is automatically exposed and cannot contain any access modifiers.
    /span>
  4. interface itself can be inherited from multiple interfaces, Classes and structs can inherit multiple interfaces, but interfaces cannot inherit classes.


3, Why can't I specify a modifier for a method in an interface?   methods in the
interface are used to define a contract for communication between objects, and it is meaningless to specify that the methods in the interface are private or protected. They default to public methods.

Interface Iprogram
{
void Fun ();
}
Class Program:iprogram
{
Explicitly implement an interface member
void Iprogram.fun ()
{
Console.WriteLine ("I am fun.");
}
Staticvoid Main (string[] args)
{
Iprogram p =new Program (); Declares an instance of an interface, but does not instantiate the interface
P.fun ();
Console.read ();
}
}

As mentioned above, implementation interfaces can be implemented explicitly and implicitly, so what are the pros and cons of these two implementations?
In general, implicit implementations can be used when a class or struct is to implement a single interface.
If a class or struct inherits multiple interfaces and the interface has the same name member, an explicit implementation is used, and the implicit implementation is invalidated when the explicit implementation method exists.

 interface Iprogram 
{
void fun ();

Interface Iaprogram
{
void fun ();
}
Class Program:iprogram, Iaprogram
{
void Iprogram.fun ()//explicitly implement interface Iprogram
{
Console.WriteLine ("I am iprogram fun.");
}
Void Iaprogram.fun ()//explicitly implements interface Iaprogram
{
Console.WriteLine ("I am Iapro Gram fun. ");
}
//public void Fun ()//implicitly implements an interface
//{
//Console.WriteLine ("I AM program Fu N. ");
//}
Staticvoid Main (string[] args)
{
//iprogram p = new program ();
P.fun ();
//iaprogram ap = new program ();
//ap. Fun ();
Program Pro =new program ();
((iprogram) pro). Fun ();
((Iaprogram) pro). Fun ();
Console.read ();
}
}

The result is: I am iprogram fun.
I am iaprogram fun.
4, the inheritance of the interface:
Interface inheritance differs from class inheritance: First, class inheritance is not only a description of inheritance, but also an implementation inheritance, whereas interface inheritance is simply a description of inheritance.
That is, a derived class can inherit a method implementation of a base class, whereas a derived interface inherits only the member method description of the parent interface and does not inherit the implementation of the parent interface.
Second, class inheritance in C # allows only single inheritance, but interface inheritance allows multiple inheritance, and a sub-interface can have multiple parent interfaces.
Interfaces can inherit from zero or more interfaces. When inheriting from multiple interfaces, use ":" followed by the inherited interface name, with "," split between multiple interface names.
The inherited interface should be accessible, such as inheriting from a private type or an interface of type internal, which is not allowed.
Interfaces are not allowed to inherit directly or indirectly from themselves. Like inheritance of classes, the inheritance of interfaces also forms a hierarchy between interfaces.

    Interface Iprogram
{
void Fun ();
}
Interface Iaprogram:iprogram
{

}
Class Program: Iaprogram
{
void Iprogram.fun ()
{
Console.WriteLine ("I am iprogram fun.");
}
Staticvoid Main (string[] args)
{
Program Pro =new program ();
((Iaprogram) pro). Fun ();
Console.read ();
}
}

5, the interface of the coverage:
Since there is no method body for the implementation of the interface, there is no method body for the abstract method, so how does it execute when we invoke the abstract method in the implementation of the interface?

    Interface Iprogram
{
void Fun ();
}
AbstractClass Aprogram:iprogram
{
Publicabstractvoid Afun ();
void Iprogram.fun ()
{
Afun ();
}
}
Class Program:aprogram
{
Publicoverridevoid Afun ()
{
Console.WriteLine ("I am Aprogram.");
}
Staticvoid Main (string[] args)
{
Iprogram Pro =new program ();
Pro. Fun ();
Console.read ();
}
}
Results: I am Aprogram.

Through the breakpoint, you can see when the pro is executed. Fun (); When you first jump to the implementation of the interface, and then call the implementation of the abstract function, when the method of the abstract function is implemented, and then back to the implementation of the interface, until the completion of execution.
When we invoke the virtual function in the method that implements the interface?

    Interface Iprogram
{
void Fun ();
}
Class Aprogram:iprogram
{
Publicvirtualvoid afun () //Note this is a virtual function
{
Console.WriteLine ("I am virtual Afun.");
}
void Iprogram.fun ()
{
Afun ();
}
}
Class Program:aprogram
{
Publicoverridevoid afun () //Here is override rewrite
{
Console.WriteLine ("I am override Afun.");
}
Staticvoid Main (string[] args)
{
Iprogram Pro =new program ();
Pro. Fun ();
Console.read ();
}
}

At this point, we find that the order of execution is the same as the previous example. So the result is: I am override Afun.
As a result, we can continue to associate when we replace the override keyword with new? Is it the same result, or is it the same as the example we used to say?
6, we make the above example to improve:

Interface Iprogram
{
void Fun ();
}
Class Aprogram:iprogram
{
Publicvirtualvoid Afun ()
{
Console.WriteLine ("I am virtual Afun.");
}
void Iprogram.fun ()
{
Afun ();
}
}
Class Program:aprogram
{
Publicnewvoid Afun ()
{
Console.WriteLine ("I am new Afun.");
}
Staticvoid Main (string[] args)
{
Program Pro =new program ();
((Iprogram) pro). Fun ();
Pro. Afun ();
Console.read ();
}
}

The result is: I am Virtual Afun.
I am New Afun.
As already mentioned, this is not analyzed here, so we know that using the New keyword is to hide it, when the method of implementation of the interface is called virtual method, and the execution of the class is the same.


7. 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. Abstract classes can have implementations of methods, and non-static class variables can be defined.
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.


8. What are the similarities and differences between the interfaces and classes in C #.
Vary
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.
With:
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

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.