1 public static void WriteLine(string format, params object[] arg);2 public static void WriteLine(string format, object arg0);3 public static void WriteLine(string format, object arg0, object arg1);4 public static void WriteLine(string format, object arg0, object arg1, object arg2);5 public static void WriteLine(string format, object arg0, object arg1, object arg2, object arg3);第一個重載能夠替代后面的所有重載,為什么還需要后面的重載呢?JaredPar給出了相關解答:1. params參數無法在間接方法綁定中自由的使用,如以下代碼:
delegate void E(string format, object o1);E e = Console.WriteLine;
如在之后按params重載的思路,使用e("{0}, {1}", "a", "b")將無法編譯通過。對于指定參數的委托,如需要使用到WriteLine,非params的重載是應該存在的;
2. 并非所有的CLI都支持params的語法;
3. 從性能的角度來說,每次調用public static void WriteLine(string format, params object[] arg),都將分配托管堆空間用于存儲對象,哪怕是如下代碼:
static void Main(string[] args){ DemoFunc("a", "b", "c"); DemoFunc("a", "b", "c");}public static void DemoFunc(params string[] val){}.method PRivate hidebysig static void Main(string[] args) cil managed{ .entrypoint // 代碼大小 78 (0x4e) .maxstack 3 .locals init ([0] string[] CS$0$0000) IL_0000: nop IL_0001: ldc.i4.3 IL_0002: newarr [mscorlib]System.String IL_0007: stloc.0 IL_0008: ldloc.0 IL_0009: ldc.i4.0 IL_000a: ldstr "a" IL_000f: stelem.ref IL_0010: ldloc.0 IL_0011: ldc.i4.1 IL_0012: ldstr "b" IL_0017: stelem.ref IL_0018: ldloc.0 IL_0019: ldc.i4.2 IL_001a: ldstr "c" IL_001f: stelem.ref IL_0020: ldloc.0 IL_0021: call void ConsoleapplicationDemo2.Program::DemoFunc(string[]) IL_0026: nop IL_0027: ldc.i4.3 IL_0028: newarr [mscorlib]System.String IL_002d: stloc.0 IL_002e: ldloc.0 IL_002f: ldc.i4.0 IL_0030: ldstr "a" IL_0035: stelem.ref IL_0036: ldloc.0 IL_0037: ldc.i4.1 IL_0038: ldstr "b" IL_003d: stelem.ref IL_003e: ldloc.0 IL_003f: ldc.i4.2 IL_0040: ldstr "c" IL_0045: stelem.ref IL_0046: ldloc.0 IL_0047: call void ConsoleApplicationDemo2.Program::DemoFunc(string[]) IL_004c: nop IL_004d: ret} // end of method Program::Main從IL中可以看出,.NET都會重復分配托管堆空間。在此,由于程序較為簡單,對于GC的壓力較小,但當程序趨于復雜時,過多的分配托管堆空間對GC的壓力是很大的,而GC往往會成為復雜程序效率的瓶頸。
新聞熱點
疑難解答