C#程式碼優化紀錄(不定期更新)
一:運用StringBuilder類別
使用StringBuilder來做字串的連結及使用
using System.Text;
const string theShowStr = "Show Str";
StringBuilder theResultBuilder = new StringBuilder();
theResultBuilder.AppendFormat("Show : {0}",theShowStr);
theResultBuilder.AppendLine(theShowStr);
theResultBuilder.Append(theShowStr);
Response.Write(theResultBuilder.ToString());
也可使用Insert 和 Remove來使用
二:運用Dictionary<TKey,TValue>
使用Dictionary查找資料
using System.Collections.Generic;
Dictionary<string, string> theResultDict = new Dictionary<string, string>()
{
{"1", "First"},
{"2", "Second"},
{"3", "Third"},
{"4", "Fourth"},
};
var theEnum = theResultDict.GetEnumerator();
while (theEnum.MoveNext())
{
theResultBuilder.AppendFormat("Key:{0}, Value : {1}", theEnum.Current.Key, theEnum.Current.Value);
}
Response.Write(theResultBuilder.ToString());
三:字串檢查
字串值使用string.IsNullOrEmpty(string) 來檢查
四:避免在迴圈裡宣告變數
C#程式開發要遵循的一個基本原則就是避免不必要的物件建立
//避免
for(int i = 0 ; i < 5 ; ++i){
Class theClass = new Class();
}
//使用
Class theClass=null;
for(int i = 0 ; i < 5 ; ++i){
theClass = new Class();
}
五:switch優化
範例出處:打造可維護軟體-C#版
給定一個國家GetFlagColors 回傳該國家國旗顏色
using System.Collections.Generic;
using System.Drawing;
public class NationalityColor
{
public enum Nationality
{
DUTCH,
GERMAN,
BELGIAN,
FRENCH,
ITALIAN,
}
public IList<Color> GetFlagColors(Nationality nationality)
{
List<Color> result = new List<Color>();
switch (nationality)
{
case Nationality.DUTCH:
result = new List<Color> { Color.Red, Color.White, Color.Blue };
break;
case Nationality.GERMAN:
result = new List<Color> { Color.Black, Color.Red, Color.Yellow };
break;
case Nationality.BELGIAN:
result = new List<Color> { Color.Black, Color.Yellow, Color.Red };
break;
case Nationality.FRENCH:
result = new List<Color> { Color.Blue, Color.White, Color.Red };
break;
case Nationality.ITALIAN:
result = new List<Color> { Color.Green, Color.White, Color.Red };
break;
default:
result = new List<Color> { Color.Gray };
break;
}
return result;
}
}使用Dictionary 重構此程式碼
using System.Collections.Generic;
using System.Drawing;
public class NationalityColor
{
public enum Nationality
{
DUTCH,
GERMAN,
BELGIAN,
FRENCH,
ITALIAN,
}
public Dictionary<Nationality, IList<Color>> FLAGS = new Dictionary<Nationality, IList<Color>>();
void FlagFactoryWithMap()
{
FLAGS[Nationality.DUTCH] = new List<Color> { Color.Red, Color.White, Color.Blue };
FLAGS[Nationality.GERMAN] = new List<Color> { Color.Black, Color.Red, Color.Yellow };
FLAGS[Nationality.BELGIAN] = new List<Color> { Color.Black, Color.Yellow, Color.Red };
FLAGS[Nationality.FRENCH] = new List<Color> { Color.Blue, Color.White, Color.Red };
FLAGS[Nationality.ITALIAN] = new List<Color> { Color.Green, Color.White, Color.Red };
}
public IList<Color> GetFlagColors(Nationality nationality)
{
IList<Color> result;
if (FLAGS.TryGetValue(nationality, out result))
{
return result;
}
return new List<Color> { Color.Gray }; ;
}
}
六:避免取值及解析字串錯誤造成程式碼崩潰
Dictionary取值時建議使用:
bool isHaveValue = Dictionary.TryGetValue(TKey key, out TValue value);
字串轉數字型別:
int.TryParse(string s,out int result);