

十二
5
public string GB2312ToUtf8(string gb2312String) { Encoding fromEncoding = Encoding.GetEncoding(“gb2312″); Encoding toEncoding = Encoding.UTF8; return EncodingConvert(gb2312String, fromEncoding, toEncoding); }
public string Utf8ToGB2312(string utf8String)
public string EncodingConvert(string fromString, Encoding fromEncoding, Encoding toEncoding) string toString = toEncoding.GetString(toBytes); |
大家都知道,代码中出现过多的if…else嵌套语句时很难读,所以常用switch…case语句进行代替,代码的可读性就好了很多。
举个简单的例子:
01 |
public void DoSomeThing(string str) |
02 |
{ |
03 |
if (str == "A") { |
04 |
DoSomethingForA(str); |
05 |
} |
06 |
else if (str == "B") { |
07 |
DoSomethingForB(str); |
08 |
} |
09 |
else if (str == "C") { |
10 |
DoSomethingForC(str); |
11 |
} |
12 |
} |
1 |
我们一眼就看出,这段代码可以使用switch语句进行重构,重构后的代码如下: |
01 |
public void DoSomeThing(string str) |
02 |
{ |
03 |
switch (str) { |
04 |
case "A": |
05 |
DoSomethingForA(str); |
06 |
break; |
07 |
case "B": |
08 |
DoSomethingForB(str); |
09 |
break; |
10 |
case "C": |
11 |
DoSomethingForC(str); |
12 |
break; |
13 |
default: |
14 |
break; |
15 |
} |
16 |
} |
1 |
这样看下去,代码确实清晰了很多。这种重构方法我们很多人都会这么做,而且效果还不错。上面的例子中代码行还很少,实际当中有可能会出现很多的case语句, |
1 |
即使我们这样重构,在阅读的时候还是需要拉很长的滚动条才可以看到需要找的case语句对应的代码。所以,针对这种情况,我们还可以用更简洁的方法进行重构。 |
1 |
实现起来也挺简单的,即:建立全局的字典类型的变量,保存不同的case语句条件以及对应的方法,这样,在需要调用的时候直接查找到该方法,invoke即可,其实 |
1 |
也是用了委托来实现。看以下代码: |
1 |
private static Dictionary<string, Action<string>> dict = new Dictionary<string, Action<string>>(); |
2 |
|
3 |
public BuildDictionary() |
4 |
{ |
5 |
dict.Add("A", DoSomethingForA); |
6 |
dict.Add("B", DoSomethingForB); |
7 |
dict.Add("C", DoSomethingForC); |
8 |
} |
1 |
上面是声明变量及填充字典,做好这部分的工作之后,调用起来就方便多了,同样的DoSomeThing方法,代码就少了很多 |
1 |
public void DoSomeThing(string str) |
2 |
{ |
3 |
if (dict.ContainsKey(str)) { |
4 |
Action<string> action = dict[str]; |
5 |
action.Invoke(str); |
6 |
} |
7 |
} |
重构之后,是不是简洁了很多,即使再多的case语句,我们也只需要在BuildDictionary方法中把它添加进去就可以了。
那么,是不是所有的switch…case语句都可以这样重构呢,以这样的实现方式来看,需要case条件对应的方法参数类型相同,这样才容易构造出字典来保存。
如果方法还要返回值,那我们就不能用Action,需要用Func表达式来保存,因为它可以带有返回值。
以上,希望对大家有所帮助。

