Many extensions have been implemented since the beginning of the series of articles, but too many extensions will cause us a lot of trouble. tips:
In the face of so many "flood" extensions, many people will feel awkward. There is indeed a sense of "overwhelming competition against the Lord", and it is too difficult to find out the methods they really want to use! Although the extended string class is "powerful", it is really easy to use.
As a result, many people feel that expansion should be appropriate and should not continue... in fact, this is an attitude to escape the problem. We should take the initiative to solve the problem, instead of avoiding it!
There are many ways to solve the above problem. The simplest thing is to put the extension into different namespaces and use the corresponding namespace as needed, which can achieve a certain effect. However, this method has many disadvantages: if there are too many extensions in a namespace, our smart prompts will be filled with the extension method. If there are too few extensions, multiple namespaces will be using each time we use them, very troublesome.
First, we will introduce a simple method to see the effect first:
In Figure 1, the first three extensions starting with As are the three extensions following the grouping technology, namely Chinese processing, conversion operations, and regular operations, the following three figures show the specific applications for these three types of extensions. In Figure 2, three extensions of Chinese processing, ToDBC, ToSBC, and GetChineseSpell, are converted to halfwidth, fullwidth, and the first letter of pinyin.
After grouping, the phenomenon of extended flood in the smart prompts of the string class is solved. The use of AsXXX starts with the letter A and will appear at the beginning of the prompt, different from native methods.
Using this method has several disadvantages:
1. to use an extension, you must first use the As command and then use the specific extension. There is one more operation than before: This is inevitable for group management. It is recommended that you directly extend the extension to the string class at a very high frequency, do not group. Only groups instances that are less frequently used.
2. The extended smart prompts are unfriendly. The extended methods are mixed with Equals and ToString, and there is no indication of the extended method.
The implementation reference code of this method is provided first, and then improved:
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;
10 public ChineseString (string s) {this. s = s ;}
11 // switch to full angle
12 public string ToSBC (string input) {throw new NotImplementedException ();}
13 // turn to halfwidth
14 public string ToDBC (string input) {throw new NotImplementedException ();}
15 // obtain the first letter of the Chinese alphabet
16