首页 | 麦客学吧 | 视频教程 | FLASH小游戏 | 素材下载 | 常用工具
您当前的位置:首页 > 程序开发 > C# > 正文

一点C#代码的使用心得

出处:麦客学吧 [2007-6-28 17:26:24] 作者:佚名 点击数:176

讨论交流:http://x8.maicoo.com/team/8667.html

好久没有写技术文章了,今天就写一点点关于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.
代码虽然越来越多,但安全性却是越来越高。总之,代码能省的地方就该省,不能省的,一个也不能少。

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

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中的函数时,

【责任编辑: lanier

关于 一点C#代码的使用心得 的相关文章
素材中心
麦客酷站赏析频道

精彩图文推荐

关于本站 - 联系站长 - 广告服务 - 合作伙伴 - 网站地图 - 版权声明 - 报告错误 - 收藏本站 | Http://www.MaiCoo.com

Copyright © 2005 - 2008 MaiCoo.com All Rights Reserved

违法和不良信息举报中心 本站服务器空间和带宽由雷克斯网络提供赞助
浙ICP备06017818号