一点C#代码的使用心得

浏览:59日期:2023-03-27

好久没有写技术文章了,今天就写一点点关于C#的使用心得吧。

1、代码问题:以前我总是这样写代码:

一点C#代码的使用心得//m_isSomeEvent:boolif(m_isSomeEvent){m_isSomeEvent;=false}else{m_isSomeEvent;=true}

后来这样写:

一点C#代码的使用心得m_isSomeEvent;=m_isSomeEvent?false:true

再后来这样写:

一点C#代码的使用心得m_isSomeEvent;=!m_isSomeEvent;

类似的有:

if(this.m_button.Text==i_someString){this.m_button.Enabled;=true}else{this.m_button.Enabled;=false}

后来就写成:

一点C#代码的使用心得this.m_button.Enabled;=this.m_button.Text;==i_someString;

有什么区别吗?没有,只能说我是越来越懒了。

字符串问题:以前总是这样写:

一点C#代码的使用心得stringm_path;="c:test"+"MyFolder"+"someFile.dat"

后来会这样写:

一点C#代码的使用心得stringm_path;=string.Format("{0}{1}{2}",i_drive,i_path,i_file);

再后来这样写:

一点C#代码的使用心得stringm_path;=Path.Combine(Path.Combine(i_drive,i_path),i_file);

虽然有点麻烦,但比起因为路径出错而造成的麻烦,这算不了什么。还有就是,以前这样写:

一点C#代码的使用心得stringm_filePath;=".myFile.dat"//在程序正在运行的目录里取文件。

后来这样写:

一点C#代码的使用心得stringm_filePath;=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"myFile.dat");

理由就不用说了,安全第一。还有一个就是:

一点C#代码的使用心得stringm_fullPath;="c:test1test2file.dat"一点C#代码的使用心得//Some;code;withe;the;path;to;create;the;file.

后来总要这样:

一点C#代码的使用心得stringm_fullPath;="c:test1test2file.dat"一点C#代码的使用心得if(!Directory.Exists(Path.GetDirectoryName(m_fullPath))){Directory.CreateDirectory(Path.GetDirectoryName(m_fullPath));}一点C#代码的使用心得//Some;code;withe;the;path;to;create;the;file.

再后来:

一点C#代码的使用心得stringm_fullPath;="c:test1test2file.dat"一点C#代码的使用心得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);}}一点C#代码的使用心得//Some;code;withe;the;path;to;create;the;file.

代码虽然越来越多,但安全性却是越来越高。总之,代码能省的地方就该省,不能省的,一个也不能少。

还有这样的问题:以前这样写函数:

publicvoidSomeFunction(objecti_someObject){//}

后来一般情况我都会先选择这样的代码:

publicvoidSomeFunction(refobjecti_someObject){//}一点C#代码的使用心得

还有一个小问题,就是我喜欢在所有的成员使用上加上this,因为这样可以直接知道它是成员还是函数内的局部变量。

2、再讨论一个try-catch结结构:以前这样写:模块A中的某函数:

publicobjectSomeFunction(refobjecti_someParameter){SomeObject;m_tempObject;=newSomeObject();;//m_tempObect;need;release;after;use;it.objectm_result;=null//Some;code;with;SomeObject;m_tempObject;m_tempObject.Dispose();returnm_result;}一点C#代码的使用心得//模块B中的调用:一点C#代码的使用心得objectm_myObject;=SomeFunction(refm_somePar);

后来遇到问题,在调用时不得不这样:

一点C#代码的使用心得objectm_myObject;=nulltry{m_myObject;;=SomeFunction(refm_somePar);}catch(Exception;ex){//Some;code;}

然而,这样问题就来了,当调用SomeFunction出现异常后,SomeFunction中的m_tempObject对象根本没有机会调用Dispose来释放资源。于是修改代码为:模块A中的函数:

publicobjectSomeFunction(refobjecti_someParameter){SomeObject;m_tempObject;=newSomeObject();;//m_tempObect;need;release;after;use;it.objectm_result;=nulltry{//Some;code;with;SomeObject;m_tempObject;}catch(Exception;ex){m_result;=null//some;code}finally{m_tempObject.Dispose();}returnm_result;}

模块B中的调用:

一点C#代码的使用心得objectm_myObject;=SomeFunction(refm_somePar);if(m_myObject;==null){//some;code}else{//some;code}

然而这样还是有问题,就是你不知道调用模块A中的函数时,当返回null后,A中到底出现了什么问题。也就是说,这里我想让B模块来Catch异常,而不想让A模块来处理。简单的办法是在A模块的函数中catch到异常后,重新再抛出一个新异常:

publicobjectSomeFunction(refobjecti_someParameter){SomeObject;m_tempObject;=newSomeObject();;//m_tempObect;need;release;after;use;it.objectm_result;=nulltry{//Some;code;with;SomeObject;m_tempObject;}catch(Exception;ex){m_result;=null//some;codethrownewException("Some;message");}finally{m_tempObject.Dispose();}returnm_result;}

这样B模块中可以知道A中发生了什么事情,从而进一步处理。然而这样的问题是:系统性能下降和异常类的改变。当然,如果直接抛出原来的异常也行,但那样没有必要,后来这样改代码:模块A的函数:

publicobjectSomeFunction(refobjecti_someParameter){using(SomeObject;m_tempObject;=newSomeObject()){objectm_result;=null//some;code;with;m_tempObject;returnm_result;}}一点C#代码的使用心得//模块B的调用:一点C#代码的使用心得objectm_myObject;=nulltry{m_myObject;;=SomeFunction(refm_somePar);}catch(Exception;ex){//Some;code;}

虽然B中还是用到了try-catch结构,但意义是不一样的。如果A是不可知模块,例如你是A模块提供方,那么这样的方法给你的用户提供了很好的灵活性。如果你是A模块的使用方,那么你完全可以自己控制try-catch结构。

相关文章: