C # Extension Method stickers)

Source: Internet
Author: User
So what is the role of the extension method? Many times we want to extend some functions for an existing class, but there is no need to inherit the class. Sometimes this class does not allow inheritance at all, such as the string class, in this case, you can use the extension method to expand its functions.
The following is an example:

1. Create a class library as follows:
 

Code:
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;

     

  3. UsingSystem. LINQ;
  4. UsingSystem. text;
  5. NamespaceMethodextendclasses
  6. {
  7. // This class does not play any role, but loads methods into the memory at runtime.
  8. Public Static ClassExtendsstring
  9. {
  10. // Use the this keyword to modify the string keyword, indicating that this method is a string extension method.
  11. Public Static StringSpacetounderline (This StringSource)
  12. {
  13. Char[] Chars = source. tochararray ();
  14. StringTemp ="";
  15. Foreach(CharCInChars)
  16. {
  17. If(Char. Iswhitespace (c ))
  18. Temp + ="_";
  19. Else
  20. Temp + = C;
  21. }
  22. ReturnTemp;
  23. }
  24. }
  25. }

Then compile.

2. Create a test ConsoleProgram, Add a reference to the previous project, and thenCodeModify as follows:
 

Code:
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. LINQ;
  4. UsingSystem. text;
  5. UsingMethodextendclasses;// This namespace is where the extension method is located
  6.  

     

    NamespaceLeleapplication1

  7. {
  8. ClassProgram
  9. {
  10. Static VoidMain (String[] ARGs)
  11. {
  12. StringSTR ="ABC dd TT";
  13. STR = Str. spacetounderline ();// The spacetounderline () method is the extension method we added for the string. It seems that the string class has multiple methods. In fact, we do not actually operate the string class.
  14. Console. writeline (STR );
  15. }
  16. }
  17. }

Compile and run the program. The result is abc_dd_tt. It indicates that the spacetounderline () method plays a role.

Summary

Pay attention to the following points during method extension:
1. The class where the extension method is located must be static and must be defined on top-level static classes.
2. The extension method must be static.
3. The parameter type of the extension method must be the type to be extended.
4. The this keyword must be added before the parameter type.

 

Original address:

http://student.csdn.net/space.php? Uid = 1068338 & Do = Blog & id = 43677

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.