一點C#代碼的使用心得

2010-08-28 10:50:35來源:西部e網(wǎng)作者:

好久沒有寫技術(shù)文章了,今天就寫一點點關(guān)于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;

有什么區(qū)別嗎?沒有,只能說我是越來越懶了。

字符串問題:
以前總是這樣寫:

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能省的地方就該省,不能省的,一個也不能少。

還有這樣的問題:
以前這樣寫函數(shù):

public void SomeFunction(object i_someObject){
//
}

后來一般情況我都會先選擇這樣的代碼:

public void SomeFunction(ref object i_someObject){
//
}

還有一個小問題,就是我喜歡在所有的成員使用上加上this,因為這樣可以直接知道它是成員還是函數(shù)內(nèi)的局部變量。

2、再討論一個try-catch結(jié)結(jié)構(gòu):
以前這樣寫:
模塊A中的某函數(shù):

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中的調(diào)用:
object m_myObject = SomeFunction(ref m_somePar);

后來遇到問題,在調(diào)用時不得不這樣:

object m_myObject = null;
try{
 m_myObject  
= SomeFunction(ref m_somePar);
}
catch(Exception ex){
 
//Some code;
}

然而,這樣問題就來了,當調(diào)用SomeFunction出現(xiàn)異常后,SomeFunction中的m_tempObject對象根本沒有機會調(diào)用Dispose來釋放資源。
于是修改代碼為:
模塊A中的函數(shù):

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中的調(diào)用:

object m_myObject = SomeFunction(ref m_somePar);
if(m_myObject ==null){
 
//some code
}
else{
 
//some code
}

然而這樣還是有問題,就是你不知道調(diào)用模塊A中的函數(shù)時,當返回null后,A中到底出現(xiàn)了什么問題。
也就是說,這里我想讓B模塊來Catch異常,而不想讓A模塊來處理。
簡單的辦法是在A模塊的函數(shù)中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中發(fā)生了什么事情,從而進一步處理。然而這樣的問題是:
系統(tǒng)性能下降和異常類的改變。當然,如果直接拋出原來的異常也行,但那樣沒有必要,后來這樣改代碼:
模塊A的函數(shù):

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的調(diào)用:
object m_myObject = null;
try{
 m_myObject  
= SomeFunction(ref m_somePar);
}
catch(Exception ex){
 
//Some code;
}

雖然B中還是用到了try-catch結(jié)構(gòu),但意義是不一樣的。如果A是不可知模塊,例如你是A模塊提供方,那么這樣的方法給你的用戶提供了很好的靈活性。
如果你是A模塊的使用方,那么你完全可以自己控制try-catch結(jié)構(gòu)。

好了,先就這一點點心得。有時間再寫一些。

原文地址:http://www.cnblogs.com/WuCountry/archive/2006/11/24/570719.html

關(guān)鍵詞:C#