C # Extension Method

Source: Internet
Author: User
C #3.0 provides this mechanism to add attributes or methods to classes, such as adding a method to strings.

Namespace myextensionmethods {
Public static class myextensions {
Public static int mygetlength (this system. String target)
{
Return target. length;
}
}
}

 

Grouping method:

Public static class stringextension
{
Public static chinesestring aschinesestring (this string s) {return New chinesestring (s );}
Public static convertablestring asconvertablestring (this string s) {return New convertablestring (s );}
Public static regexablestring asregexablestring (this string s) {return New regexablestring (s );}
}
Public class chinesestring
{
Private string S;
Public chinesestring (string s) {This. S = s ;}
// Convert the fullwidth
Public String tosbc (string input) {Throw new notimplementedexception ();}
// Turn to the halfwidth
Public String todbc (string input) {Throw new notimplementedexception ();}
// Obtain the first letter of the Chinese alphabet
Public String getchinesespell (string input) {Throw new notimplementedexception ();}
}
Public class convertablestring
{
Private string S;
Public convertablestring (string s) {This. S = s ;}
Public bool isint (string s) {Throw new notimplementedexception ();}
Public bool isdatetime (string s) {Throw new notimplementedexception ();}
Public int toint (string s) {Throw new notimplementedexception ();}
Public datetime todatetime (string s) {Throw new notimplementedexception ();}
}
Public class regexablestring
{
Private string S;
Public regexablestring (string s) {This. S = s ;}
Public bool ismatch (string S, string pattern) {Throw new notimplementedexception ();}
Public String Match (string S, string pattern) {Throw new notimplementedexception ();}
Public String relplace (string S, string pattern, matchevaluator evaluator) {Throw new notimplementedexception ();}
}

 

You need to introduce this namespace when using it. The reference is as follows:

C # code
 StringStr= "Dafasdf";IntLen=Str. mygetlength (); 

Declaration method of the extension method:
1. Static class, static method
2. The first parameter is an instance of the target class. You need to use the this keyword.

For other questions, see. Net 3.0 features. (Use vs2008)

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.