zoukankan      html  css  js  c++  java
  • 一点C#代码的使用心得

    好久没有写技术文章了,今天就写一点点关于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中的函数时,当返回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结构。

    好了,先就这一点点心得。有时间再写一些。

  • 相关阅读:
    理解全虚拟、半虚拟以及硬件辅助的虚拟化
    使用PowerShell向SharePoint中写入数据
    Python的时间模块小结(转自:不懂真人)
    Windows下如何安装Python的第三方库
    Python的正则表达式笔记
    PG, Pool之间的一些数量关系
    Ceph与OpenStack的Nova相结合
    rbd命令
    rados命令
    ceph命令
  • 原文地址:https://www.cnblogs.com/WuCountry/p/570719.html
Copyright © 2011-2022 走看看