Extending interfaces using extension methods in c#3.0

Source: Internet
Author: User
Tags readline static class
Some knowledge points about extension methods see my previous blog: extension methods in c#3.0 (Extension Methods)
In my previous blog I highlighted two special scenarios:
1, the extension method and the original class method of the same time processing logic
2. Nesting of extension methods
Now let's look at the third scenario that the extension method uses: Extending the interface using an extension method

Examples of application scenarios:
We sometimes find an interface that is initially defined, in which case the interface needs to add another function. With the extension method, we have one more implementation option in this case. Here's a code to illustrate this problem:
Namespace Hongjun.guo
{
Interface MyInterface
{
void Test01 ();
}
public class Myclass:myinterface
{
public void Test01 ()
{
Console.WriteLine ("Test01");
}
} Static Class Myextensionmethods
{
public static void MyTest002 (this MyInterface II)
{
Ii. TEST01 ();
} } }

Call Method:
Using Hongjun.guo;
static void Main (string[] args)
{
MyInterface mm = new MyClass ();
Mm. MyTest002 ();
Console.ReadLine ();
}
Analyze the benefits of doing this:
1, if we implement the MyInterface interface a lot of classes, these classes do not derive relations, this time we want to add a function on the interface, according to the previous practice, the implementation of the interface of the number of classes, we need to change how many, using the extension method, we only need to change a place. Reduce the amount of code. If the classes that implement this interface are encapsulated in different components, and some components are difficult to modify for other reasons, then extending the interface with extension methods really gives us a dose of medicine.
2. The extension method is invoked, provided that the namespace in which the extension method resides is used. If we put the interface in the same namespace as the extension method, the extension method needs to refer to the namespace problem and it doesn't exist. Because you have to use this interface, you will necessarily reference this namespace.
3, extension method extension of the interface, and you implement the interface of the class itself to achieve the name of the same problem (this time also can be simply thought that this class override the function implementation).
First of all, there is no conflict between the two. If you are an interface call, it is an extension method, if the implementation of class invocation, it is the implementation of the class itself, see my previous blog on the extension method and the original class method of the same time processing logic description, the following code to describe the problem.
Namespace Hongjun.guo
{
Interface MyInterface
{
void Test01 ();
}
Static Class Myextensionmethods
{
public static void MyTest002 (this MyInterface II)
{
Ii. TEST01 ();
} }
public class Myclass:myinterface
{
public void Test01 ()
{
Console.WriteLine ("Test01");
} public void MyTest002 ()
{
Console.WriteLine ("MyTest002");
}
}
}
Calling code:
Using Hongjun.guo;
static void Main (string[] args)
{
MyInterface mm = new MyClass ();
Mm. MyTest002 ();
Console.WriteLine ("***********");
((MyClass) mm). MyTest002 (); Console.ReadLine ();
}
The result of this code execution is Test01
***********
MYTEST002 Analysis:
Mm. MyTest002 (); This line of code is an interface call, then the extension method is used.
((MyClass) mm). MyTest002 (); This line of code caller type is MyClass, MyClass implements the MYTEST002 function, then it is not an extension method call.

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.