Example analysis of extension methods in C #

Source: Internet
Author: User
This article mainly introduces the C # extension method, combined with the example form analysis of C # extension method of the function, the use of methods and related considerations, the need for friends can refer to the following

This example describes the C # extension method. Share to everyone for your reference, as follows:

Extension methods

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. For client code written in C # and Visual Basic, there is no significant difference between calling extension methods and calling methods that are actually defined in the type.

If we had a requirement to convert the first character of a string to uppercase, the second character to the nth character to lowercase, the other unchanged, how do we do it?

Do not use extension methods:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace extramethod{  //abstract static Stringhelper class public  static class Stringhelper  {    //abstract to capitalize the first character of a string, From the first to the Len lowercase, the other immutable method public    static string topascal (String S,int len)    {      return s.substring (0, 1). ToUpper () + s.substring (1, Len). ToLower () + s.substring (len + 1);    }  }  Class program  {    static void Main (string[] args)    {      string s1 = "ASDDADFGDFSF";      String s2 = "SBFSDFFSJG";      Console.WriteLine (Stringhelper.topascal (s1,3));      Console.WriteLine (stringhelper.topascal (S2, 5));}}}  

To use the extension method:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace extramethod{  class program  {    static void Main (string[] args)    {      string s1 = "ASDDADFGDFSF ";      String s2 = "SBFSDFFSJG";      Console.WriteLine (S1. Topascal (3));      Console.WriteLine (S2. Topascal (5));    }  }  The extension class, as long as it is static, can be public  static class Extraclass  {    //extension Method-Special static Method--Add a special method for string type topascal    public static string Topascal (this string s, int len)    {      return s.substring (0, 1). ToUpper () + s.substring (1, Len). ToLower () + s.substring (len + 1);}}  

By comparing the above two methods:

1. The code is more convenient to access static methods such as Topascal. Used as if the extension type does have the same instance method.
2. Extension methods do not change the code of the extended class without recompiling, modifying, deriving the extended class

Defining extension methods

1. Define a static class to contain extension methods.
2. The class must be visible to client code.
3. Implement the extension method as a static method and make it at least as visible as the containing class.
4. The first parameter of the method specifies the type that the method operates on, and the parameter must begin with the this modifier.

Note that the first parameter is not specified by the calling code because it represents the type of operator being applied, and the compiler already knows the type of the object. You only need to provide the arguments for these two parameters through N.

Precautions:

1. Extension methods must be defined in a static class
2. Extension methods have a lower priority than class methods with the same name
3. Extension methods are only valid within a specific namespace
4. Do not misuse the extension method unless necessary

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.