ReportDocument report=new ReportDocument();
ExportOptions exportOpts = new ExportOptions();
exportOpts=report.ExportOptions;
請問這些代碼與下面代碼在性能上有差別嗎?
ReportDocument report=new ReportDocument();
ExportOptions exportOpts = report.ExportOptions;

解决方案 »

  1.   

    有区别:
    如果ExportOptions的构造函数要等待把一个火箭送上天才返回的话,那第一种方法会多浪费一架火箭.
      

  2.   

    jf, 没有大差别, 上面的exportOpts创建了一个ExportOptions的新实例然后引用,下面是自身引用
      

  3.   

    jointan
    说的对,ExportOptions exportOpts = new ExportOptions();是运行了ExportOptions的构造函数
      

  4.   

    你可以按照实例分析一下:这里是一段简单的代码:
    Exception e0 = new Exception("aa");
    一:
    Exception e1 = new Exception();
    e1 = e0;
    二:
    Exception e2 = e0;IL如下:
    一:
          L_000b: newobj instance void [mscorlib]System.Exception::.ctor(string)
          L_0010: stloc.0 
          L_0011: newobj instance void [mscorlib]System.Exception::.ctor()
          L_0016: stloc.1 
          L_0017: ldloc.0 
          L_0018: stloc.1 二:
          L_000b: newobj instance void [mscorlib]System.Exception::.ctor(string)
          L_0010: stloc.0 
          L_0011: ldloc.0 
          L_0012: stloc.1 一要比二多执行了一次构造。但微软推荐的写法是第一种,而且在.Net 2.0种有所改进。
      

  5.   

    对了,上面的代码是用.Net 1.1写的
      

  6.   

    有,上面一个让费时间和空间,多调用了一次构函,如果在c++里面,代价还要付出一个copy assignment。在c#里面这个就没有了。