C # extension method imaginative Magic Advanced Chapter Four: Group Management of extensions

Source: Internet
Author: User
Tags bool static class types of extensions

From the beginning of the series to now, many of the extensions have been implemented, but too much expansion will bring us a lot of trouble, see the following figure:

In the face of so many "flood" expansion, many people will feel very awkward, indeed a "distracting" feeling, want to find out the real way to use it is too difficult! Although the extended string class is "powerful," it is very poor in ease of use.

Many people therefore feel that expansion should be enough, should not go on ... In fact, this is a problem-avoidance attitude, there are problems we should take the initiative to solve, rather than to avoid!

There are many ways to solve the above problems, the simplest is to use the extension into different namespace, use the corresponding namespace on demand, can achieve a certain effect. But there are big drawbacks to this approach: too many extensions in a namespace can also make our smart hints full of extension methods, too little to use multiple namespaces each time.

First, introduce a simple way to see the effect first:

The first three extensions in Figure 1, starting with AS, are the three types of extensions followed by group technology, namely Chinese processing, conversion operations, regular operations, and the subsequent three graphs for the specific applications of these three types of extensions. In Figure 2, there are three extensions todbc, TOSBC, Getchinesespell, which are converted to half-width, full angle, and pinyin first letter.

After this grouping, the extended overflow phenomenon in the smart tip of the string class is resolved, using asxxx, starting with the letter A, appearing at the front of the hint, and separating from the native method.

There are several drawbacks to this approach:

1. Use an extension to first as once, and then use a specific extension, more than the previous step: This is the inevitable group management, it is recommended to use a very high frequency or directly extended to the string class, do not group. group only with a low frequency.

2. The extended smart tip is unfriendly, and the extended method is mixed with equals, ToString, and there is no sign of extension methods.

First give the implementation reference code for this method, and then improve:

1 public static class Stringextension
2 {
3 public static chinesestring aschinesestring (This string s ) {return new chinesestring (s);}
4 public static convertablestring asconvertablestring (this string s) {return new convertablestring (s);}
5 public static regexablestring asregexablestring (this string s) {return new regexablestring (s);}
6}
7 public class Chinesestring
8 {
9 private string S.
Public chinesestring (string s) {THIS.S = s ;
11//Turn all corners
public string TOSBC (string input) {throw new NotImplementedException ();}
13//Half-width
Todbc (string input) {throw new NotImplementedException ();}
15//Get Chinese Pinyin first letter
public string Getchinesespell (string input) {throw new NotImplementedException ();}
17}
-public class convertablestring
{
private string S.
Public convertablestring (string s) {thi S.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 ();}
The public DateTime todatetime (string s) {throw new NotImplementedException ();}
26} 
' public class regexablestring
' {
# private string S.
Public regexablestring (string s) {THIS.S = S;
to public bool IsMatch (string s, string pattern) {throw new NotImplementedException ();}
public string Match (string s, string pattern) {throw new NotImplementedException ();}  
Relplace (string s, string pattern, MatchEvaluator evaluator) {throw new NotImplementedException ();
%}

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.