一点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);

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.cnblogs.com/WuCountry/archive/2006/11/24/570719.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值