This is the interface in C #.

Source: Internet
Author: User

Interface appears to solve the problem that multiple inheritance is not allowed in C #.

1. What is an interface?

I think the interface can be understood as a uniform naming of a set of method declarations, but these methods do not provide any implementations.

With interfaces, methods can be managed uniformly, avoiding the repetition of defining these methods in each type.

2, how to use the interface to program

2.1 Definition of the interface

Interface Icustomcompare

{

Defines a comparison method that is implemented by the class that inherits the interface

int CompareTo (object other);

}

Defining a method in an interface cannot add any access modifiers because the method in the interface is public by default, and a compile-time error occurs if the modifier is specified in the display. You cannot also decorate with the static keyword.

In addition to defining methods in an interface, you can include properties, events, indexers, or any combination of the 4 class members (including method), but the interface type cannot contain fields, operator overloads, instance constructors, and destructors.

2.2 Inheritance Interfaces

public class Person:icustomcompare

{

int age;

public int age{get{return the age;} Set{age=value;}}

Implementing interface Methods

public int CompareTo (object value)

{

if (value==null)

{

return 1;

}

Casting the object type to person type

Person otherp= (person) value;

Compare the Age property of the current object to the age property of the object you want to compare

if (this. Age<otherp. Age)

{

return-1;

}

if (this. Age>otherp. Age)

{

return 1;

}

return 0;

}

}

In the person class above, the CompareTo method in the Icustomcompare interface is implemented.

The CompareTo method first determines whether the parameter object is null, and if NULL, returns 1 directly, representing the current object larger than the incoming object.

If the object is not empty, cast to the person type, contrast the Age property, the current object has a value older than the value of the passed-in object, then returns 1, or 1, which indicates that the current object has a smaller-than-passed object.

2.3 Methods in the calling interface

Class Program

{

static void Main (string[] args)

{

Person P1=new person ();

P1. age=18;

Person P2=new person ();

P2. age=19;

Invoke the methods in the interface to compare P1 and P2

if (P1.compareto (p2) >0)

{

Console.WriteLine ("P1 greater than P2");

}

else if (P1.compareto (p2) <0)

{

Console.WriteLine ("P1 is smaller than P2");

}

Else

{

Console.WriteLine ("P1 is as big as P2");

}

Console.readkey ();

}

}

3, display interface implementation method

In the example code above, an implicit implementation of the interface is used, that is, there is no CompareTo method in the implementation code that specifies which interface to implement.

Of course, there is also an explicit interface implementation, in the process of implementing the interface of the class, explicitly indicate which of the interfaces to implement which method.

Interface ichinesegreeting

{

Method declaration

void SayHello ();

}

Interface iamericangreeting

{

Method declaration

void SayHello ();

}

The Speaker class implements two interfaces

public class Speaker:ichinesegreeting,iamericangreeting

{

An implicit interface implementation

public void SayHello ()

{

Console.WriteLine ("Hello");

}

}

The Speaker class above implements two interfaces, and it happens that the methods declared in the two interfaces have the same return type, the same method name, and the same parameters. With an implicit implementation of the interface, the following code calls the same SayHello method.

static void Main (string[] args)

{

Speaker speaker=new Speaker ();

Call Chinese Greeting method

Ichinesegreeting chinese= (ichinesegreeting) speaker;

Chinese. SayHello ();

Call American Greeting method

Iamericangreeting american= (iamericangreeting) speaker;

American.sayhello ();

}

When the main function executes, the resulting interface is two "Hello";

This is not the result we expect, so in this case we have to use an explicit interface implementation to resolve this naming conflict problem.

The Speaker class implements two interfaces

public class Speaker:ichinesegreeting,iamericangreeting

{

An explicit interface implementation

void Ichinesegreeting.sayhello ()

{

Console.WriteLine ("Hello");

}

  

An explicit interface implementation

void Iamericangreeting.sayhello ()

{

Console.WriteLine ("Hello");

}

}

You can use this method to resolve the naming conflict problem. When using the displayed interface Shanxi Ian Way, you need to pay attention to a few questions:

    • If an interface is explicitly implemented, the method cannot use any access modifiers, and explicitly implemented members are defaulted to private.
    • Explicitly implemented members are private by default, so these members cannot be accessed through the object of the class. The correct method of invocation is to explicitly convert the speaker object to the corresponding interface and invoke the SayHello method through an interface.

For the above two ways: explicit implementation and implicit implementation, the following for two implementation of the difference and use scenarios to summarize:

    • When implemented with an implicit interface, both classes and interfaces can access the methods in the interface, and if an explicit interface implementation is used, the interface method can only be accessed through the interface, so the interface method is private by default.
    • When a class implements a single interface, an implicit interface implementation is typically used so that objects of the class can directly access the interface method.
    • An explicit interface implementation should be used when a class implements multiple interfaces, and the interface contains the same method name, parameter, and return type. Even if you do not have the same method signature, it is recommended to use an explicit method when implementing multiple interfaces, as this will identify which method belongs to which interface.

4. Interface and abstract class

The main point here is to explain the difference between the two:

    • Abstract classes are defined using the abstract keyword, and the interfaces are defined using interface, none of them can be instantiated.
    • An abstract class can contain virtual methods, non-abstract methods, and static members, but the interface cannot contain virtual methods and any static members, and only methods can be defined in an interface, and there cannot be a concrete implementation, and the implementation of the method is done by the implementing class.
    • Abstract classes cannot implement multiple inheritance, and interfaces support multiple inheritance. Note that, in the strict sense, the class interface should become the class implementation interface.
    • Abstract class is an abstraction of a kind of object, inheriting from the class of abstract class and abstract class belongs to the relationship, and the class implementation interface just represents the implementation class has the interface declaration method, is a can-do relationship. So the general interface is followed by the able field, which means "I can do it".

The above points are just the main method of difference between interface and abstract class, they have a lot of different, you can refer to other books or other blogs.

This is the interface in C #.

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.