Home  >  Article  >  Backend Development  >  Three interface implementation methods in .NET

Three interface implementation methods in .NET

巴扎黑
巴扎黑Original
2016-12-20 17:19:482292browse

Generally speaking, .NET provides three different interface implementation methods, namely implicit interface implementation, explicit interface implementation, and mixed interface implementation. Each of these three methods has its own characteristics.

First, let’s look at implicit interface implementation. This is probably the most commonly used interface implementation, because implicit interface implementation is .NET’s default interface implementation. Let's look at an example of implicit interface implementation:

using System;
internal class MyClass
{
public void SomeMethod()
{
// Use the interface to declare a Myinplement object
IMyInterface iObj = new MyInplement( );
iObj.MethodA();
// Use the class method to declare a Myinplement object
MyInplement obj = new MyInplement();
obj.MethodB();
}
}
public class MyInplement : IMyInterface
{
# region IMyInterface Members
///


/// Use implicit interface implementation to implement methods in the interface
///

public void MethodA()
{
Console.WriteLine("a ");
}
///
/// Use implicit interface implementation to implement methods in the interface
///

public void MethodB()
{
Console.WriteLine( "B");
}
#endregion
}
public interface IMyInterface
{
void MethodA();
void MethodB();
}

The above code is very simple. We declare an interface and then use the MyImplement class to implement the interface. When implementing the interface, we chose implicit interface implementation, that is, adding a modifier before the method signature defined in the interface to define the signature of the implementation method, such as public void MethodB(). This implicit interface implementation is very simple to use, but one disadvantage is that it allows customers to directly call methods in the implementation class (that is, declare the object through MyInplement obj = new MyInplement ()) instead of through the interface Calling methods in the implementation class (that is, declaring objects through IMyInterface iObj = new MyInplement ();) undoubtedly increases the coupling of the code and violates the principles of interface-oriented programming. So how can we prevent customers from directly calling the implementation class? The explicit interface implementation we will look at below is enough.

Explicit interface implementation means that when the implementation class implements the interface method, it uses the "interface name. method name" style to define the method name in the signature of the method. At the same time, modifiers are not allowed in the signature of the method because All methods implemented by explicit interfaces are private, which is rigidly stipulated by .NET in order to prevent customers from directly calling the implementation class. Let's look at an example of explicit interface implementation:

using System;
internal class MyClass
{
public void SomeMethod()
{
// Use the interface to declare a Myinplement object
IMyInterface iObj = new MyInplement();
iObj.MethodA();
// Declaring a Myinplement object using a class will report an error because the MethodA method of MyImplement is private
MyInplement obj = new MyInplement();
obj.MethodB();
}
}
public class MyInplement : IMyInterface
{
#region IMyInterface Members
///


/// Use explicit interface implementation to implement the methods in the interface
///

void IMyInterface.MethodA ()
{
Console.WriteLine("a");
}
///
/// Using explicit interface implementation to implement the methods in the interface
/// will report an error because in the explicit Modifiers are not allowed when implementing the interface
///

public void IMyInterface.MethodB()
{
Console.WriteLine("B");
}
#endregion
}
public interface IMyInterface
{
void MethodA();
void MethodB();
}

There are two places where errors will be reported in the above code. This is because we have violated the constraints of explicit interface implementation where errors are reported.

Finally, let’s look at hybrid interface implementation. Hybrid interface implementation is to use implicit interface implementation and explicit interface implementation together. Let’s look at an example:

using System;
internal class MyClass
{
public void SomeMethod()
{
// Use the interface to declare a Myinplement object
IMyInterface iObj = new MyInplement();
iObj.MethodA();
// Use the class to declare a Myinplement object, no An error is reported because the MethodA method of MyImplement is public
MyInplement obj = new MyInplement();
obj.MethodB();
}
}
public class MyInplement : IMyInterface
{
#region IMyInterface Members
/// /// Use explicit interface implementation to implement methods in the interface
///
void IMyInterface.MethodA()
{
Console.WriteLine("a");
}
///


/// Use implicit interface implementation to implement methods in the interface
///

public void MethodB()
{
Console.WriteLine("B");
}
# endregion
}
public interface IMyInterface
{
void MethodA();
void MethodB();
}

In the above code, we changed the implementation of MehtodB to an implicit interface implementation, so that the modifier of MyImplement.Method is public and there is no error in the code.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# Learning ReflectionNext article:C# Learning Reflection