好久沒有寫技術文章了,今天就寫一點點關于C#的使用心得吧。
1、代碼問題:
以前我總是這樣寫代碼:
//m_isSomeEvent:bool
if(m_isSomeEvent){
m_isSomeEvent = false;
}else{
m_isSomeEvent = true;
}
后來這樣寫:
m_isSomeEvent = m_isSomeEvent?false:true;
再后來這樣寫:
m_isSomeEvent = !m_isSomeEvent;
類似的有:
if(this.m_button.Text==i_someString){
this.m_button.Enabled = true;
}else{
this.m_button.Enabled = false;
}
后來就寫成:
this.m_button.Enabled = this.m_button.Text == i_someString;
有什么區別嗎?沒有,只能說我是越來越懶了。
字符串問題:
以前總是這樣寫:
string m_path = "c://test//"+"MyFolder"+"//someFile.dat";
后來會這樣寫:
string m_path = string.Format("{0}//{1}//{2}",i_drive,i_path,i_file);
再后來這樣寫:
string m_path = Path.Combine(Path.Combine(i_drive,i_path),i_file);
雖然有點麻煩,但比起因為路徑出錯而造成的麻煩,這算不了什么。
還有就是,以前這樣寫:
string m_filePath = "./myFile.dat"; //在程序正在運行的目錄里取文件。
后來這樣寫:
string m_filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"myFile.dat");
理由就不用說了,安全第一。
還有一個就是:
string m_fullPath = "c://test1//test2//file.dat";
//Some code withe the path to create the file.
后來總要這樣:
string m_fullPath = "c://test1//test2//file.dat";
if(!Directory.Exists(Path.GetDirectoryName(m_fullPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(m_fullPath));
}
//Some code withe the path to create the file.
再后來:
string m_fullPath = "c://test1//test2//file.dat";
if(!Directory.Exists(Path.GetDirectoryName(m_fullPath)))
{
try{
Directory.CreateDirectory(Path.GetDirectoryName(m_fullPath));
}
catch(Exception ex)
{
MessageBox.Show(this,"Error! Object folder "+m_fullPath+" does't exist. And cann't create this folder. Message:"+ex.Message);
}
}
//Some code withe the path to create the file.
代碼雖然越來越多,但安全性卻是越來越高??傊a能省的地方就該省,不能省的,一個也不能少。
還有這樣的問題:
以前這樣寫函數:
public void SomeFunction(object i_someObject){
//
}
后來一般情況我都會先選擇這樣的代碼:
public void SomeFunction(ref object i_someObject){
//
}
還有一個小問題,就是我喜歡在所有的成員使用上加上this,因為這樣可以直接知道它是成員還是函數內的局部變量。
2、再討論一個try-catch結結構:
以前這樣寫:
模塊A中的某函數:
public object SomeFunction(ref object i_someParameter){
SomeObject m_tempObject = new SomeObject(); //m_tempObect need release after use it.
object m_result = null;
//Some code with SomeObject m_tempObject;
m_tempObject.Dispose();
return m_result;
}//模塊B中的調用:
object m_myObject = SomeFunction(ref m_somePar);
后來遇到問題,在調用時不得不這樣:
object m_myObject = null;
try{
m_myObject = SomeFunction(ref m_somePar);
}catch(Exception ex){
//Some code;
}
然而,這樣問題就來了,當調用SomeFunction出現異常后,SomeFunction中的m_tempObject對象根本沒有機會調用Dispose來釋放資源。
于是修改代碼為:
模塊A中的函數:
public object SomeFunction(ref object i_someParameter){
SomeObject m_tempObject = new SomeObject(); //m_tempObect need release after use it.
object m_result = null;
try{
//Some code with SomeObject m_tempObject;
}catch(Exception ex){
m_result = null;
//some code
}
finally{
m_tempObject.Dispose();
}
return m_result;
}
模塊B中的調用:
object m_myObject = SomeFunction(ref m_somePar);
if(m_myObject ==null){
//some code
}else{
//some code
}
然而這樣還是有問題,就是你不知道調用模塊A中的函數時,當返回null后,A中到底出現了什么問題。
也就是說,這里我想讓B模塊來Catch異常,而不想讓A模塊來處理。
簡單的辦法是在A模塊的函數中catch到異常后,重新再拋出一個新異常:
public object SomeFunction(ref object i_someParameter){
SomeObject m_tempObject = new SomeObject(); //m_tempObect need release after use it.
object m_result = null;
try{
//Some code with SomeObject m_tempObject;
}catch(Exception ex){
m_result = null;
//some code
throw new Exception("Some message");
}
finally{
m_tempObject.Dispose();
}
return m_result;
}
這樣B模塊中可以知道A中發生了什么事情,從而進一步處理。然而這樣的問題是:
系統性能下降和異常類的改變。當然,如果直接拋出原來的異常也行,但那樣沒有必要,后來這樣改代碼:
模塊A的函數:
public object SomeFunction(ref object i_someParameter){
using(SomeObject m_tempObject = new SomeObject()){
object m_result = null;
//some code with m_tempObject;
return m_result;
}
}
//模塊B的調用:
object m_myObject = null;
try{
m_myObject = SomeFunction(ref m_somePar);
}catch(Exception ex){
//Some code;
}
雖然B中還是用到了try-catch結構,但意義是不一樣的。如果A是不可知模塊,例如你是A模塊提供方,那么這樣的方法給你的用戶提供了很好的靈活性。
如果你是A模塊的使用方,那么你完全可以自己控制try-catch結構。
好了,先就這一點點心得。有時間再寫一些。
http://www.49028c.com/WuCountry/archive/2006/11/24/570719.html
新聞熱點
疑難解答