C # extend the system string method

Source: Internet
Author: User

------------ StringHelper. cs ------------- using System; using System. collections. generic; using System. linq; using System. text; using System. text. regularExpressions; // steps for declaring the extension method: the class must be static and the method is static. // The first parameter is the extended object, which is marked with this. // When using the extension method, you must ensure that the extension method class is already in the current Code. The extension method of usingnamespace {// The extension method must be a static public static class StringHelper {// The extension method must be static, the first parameter must contain this public static bool IsEmail (this string _ input) {return Regex. isMatch (_ input, @ "^ \ w + @ \ w + \. \ w + $ ");} // Extension Method with multiple parameters // Add the specified public static string Quot (this string _ input, string _ quot) before and after the original string) {return _ quot + _ input + _ quot ;}}------------ Program. cs ------------- using System; Using System. collections. generic; using System. linq; using System. text; namespace Extension Method {class Program {static void Main (string [] args) {string _ myEmail = "abc@163.com "; // here we can directly use the string class Extension Method IsEmail Console. writeLine (_ myEmail. isEmail (); // call the Console of the Extension Method for receiving parameters. writeLine (_ myEmail. quot ("! "); Console. ReadLine ();}}}

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.