Using extension methods in C #

Source: Internet
Author: User

in our programming process, will use a variety of libraries, have their own writing, and others. When we use third-party class libraries , sometimes in order to ease the use of the library to add some helper methods, it is necessary to use the extension method.

Look at MSDN: extension methods enable you to "add" methods to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special static method, but it can be called just like an instance method on an extended type.
In general, it is recommended that you implement extension methods only when you have to, and implement them with caution. Whenever possible, client code that must extend an existing type should achieve this by creating a new type derived from an existing type.

For more information, see Inheritance (C # Programming Guide).
When you use an extension method to extend a type that you cannot change the source code for, you need to bear the risk that the changes in that type implementation will invalidate the extension method.
If you do implement an extension method for a given type, keep in mind the following points:
If the extension method has the same signature as the method defined in the type, the extension method is never called.
The extension method is placed in scope at the namespace level. For example, if you have multiple static classes in a namespace named Extensions that contain extension methods, then these extension methods will all be made by using Extensions; Instruction is placed in scope.


Principles and procedures for extending methods:
1 The class where the method resides must be static
The 2 method must also be static
The first parameter of the 3 method must be the type you want to extend, for example if you want to extend a method to int, then the first argument must be int.
4 A This keyword is also required before the first parameter.

First step: Create a class library and test class
Namespace Testclibray
{
public class Student
{
public string Getfirstname ()
{
return "Daniel";
}
public string Getlastname ()
{
return "D";
}
}
}
Step Two: Create a new class library and helper class, and write an extension method. Note that static classes, and static methods
Namespace Helper
{
public static Class Extensionhelper
{
public static string Getfullname (this testclibray.student stu)
{
Return Stu. Getfirstname () + "." + stu. Getlastname ();
}
}
}
Part III: Testing uses this extension method: Note To refer to the class library where the extension method resides

using System;
using System.Windows.Forms;
using Testclibray;
using Helper;
Namespace WindowsFormsApplication2
{
    public partial class Form1:form
    {
& nbsp       Public Form1 ()
        {
            Initiali Zecomponent ();
       }


        private void button1_click (object sender, EventArgs e)
      &NB Sp {
            Student stu = new Student ();
            This.textBox1.Text = stu. Getfullname ();
       }
   }
}

Results


Using extension methods 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.