当前位置: 移动技术网 > IT编程>开发语言>.net > C#.NET优化

C#.NET优化

2018年10月30日  | 移动技术网IT编程  | 我要评论

忘打一成语,忧郁弟,调教女忍者

1)避免使用arraylist。
     因为任何对象添加到arraylist都要封箱为system.object类型,从arraylist取出数据时,要拆箱回实际的类型。建议使用自定义的集合类型代替arraylist。.net 2.0提供了一个新的类型,叫泛型,这是一个强类型,使用泛型集合就可以避免了封箱和拆箱的发生,提高了性能。

2)使用hashtale代替其他字典集合类型(如stringdictionary,namevaluecollection,hybridcollection),存放少量数据的时候可以使用hashtable.

3)为字符串容器声明常量,不要直接把字符封装在双引号" "里面。
      //避免
      //
      myobject obj = new myobject();
      obj.status = "active";

      //推荐
      const string c_status = "active";
      myobject obj = new myobject();
      obj.status = c_status;

4) 不要用uppercase,lowercase转换字符串进行比较,用string.compare代替,它可以忽略大小写进行比较.
   
   例:
 
      const string c_value = "compare";
      if (string.compare(svariable, c_value, true) == 0)
      {
      console.write("same");
      }


5) 用stringbuilder代替使用字符串连接符 “+”,.

      //避免
      string sxml = "<parent>";
      sxml += "<child>";
      sxml += "data";
      sxml += "</child>";
      sxml += "</parent>";

      //推荐
      stringbuilder sbxml = new stringbuilder();
      sbxml.append("<parent>");
      sbxml.append("<child>");
      sbxml.append("data");
      sbxml.append("</child>");
      sbxml.append("</parent>");

6) if you are only reading from the xml object, avoid using xmldocumentt, instead use xpathdocument, which is readonly and so improves performance. 
如果只是从xml对象读取数据,用只读的xpathdocument代替xmldocument,可以提高性能
      //避免
      xmldocument xmld = new xmldocument();
      xmld.loadxml(sxml);
      txtname.text = xmld.selectsinglenode("/packet/child").innertext;

.

      //推荐
      xpathdocument xmldcontext = new xpathdocument(new stringreader(ocontext.value));
      xpathnavigator xnav = xmldcontext.createnavigator();
      xpathnodeiterator xpnodeiter = xnav.select("packet/child");
      icount = xpnodeiter.count;
      xpnodeiter = xnav.selectdescendants(xpathnodetype.element, false); 
      while(xpnodeiter.movenext())
      {
      scurrvalues += xpnodeiter.current.value+"~"; 
      }


7) 避免在循环体里声明变量,应该在循环体外声明变量,在循环体里初始化。

      //避免
      for(int i=0; i<10; i++)
      {
      someclass objsc = new someclass();
      .
      .
      .

      }

      //推荐
      someclass objsc = null;
      for(int i=0; i<10; i++)
      {
      objsc = new someclass();
      
      .
      .
      .
      }

8) 捕获指定的异常,不要使用通用的system.exception.

      //避免
      try
      {
      <some logic>
      }
      catch(exception exc)
      {
      <error handling>
      }
      
      //推荐
      try
      {
      <some logic>
      }
      catch(system.nullreferenceexception exc)
      {
      <error handling>
      }
      catch(system.argumentoutofrangeexception exc)
      {
      <error handling>
      }
      catch(system.invalidcastexception exc)
      {
      <error handling>
      }

9) 使用try...catch...finally时, 要在finally里释放占用的资源如连接,文件流等
不然在catch到错误后占用的资源不能释放。
        
        try
      {
         ...
      }
      catch 
        {...}
        finally
        {
          conntion.close()
        }     
10) 避免使用递归调用和嵌套循环,使用他们会严重影响性能,在不得不用的时候才使用。

11) 使用适当的caching策略来提高性能

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网