没想到C#模式匹配,代码优雅的背后却这么丑陋

不同的模式匹配,IL代码一模一样,都要box2次,1次unbox,太丑陋了受不了
image

  // [15 9 - 15 27]
    IL_0000: ldarg.1      // arg
    IL_0001: box          !!0/*T*/
    IL_0006: isinst       [System.Runtime]System.Int32
    IL_000b: brfalse.s    IL_001e
    IL_000d: ldarg.1      // arg
    IL_000e: box          !!0/*T*/
    IL_0013: isinst       [System.Runtime]System.Int32
    IL_0018: unbox.any    [System.Runtime]System.Int32
    IL_001d: stloc.0      // iv

    IL_001e: ldarg.1      // arg
    IL_001f: box          !!0/*T*/
    IL_0024: isinst       [System.Runtime]System.Int32
    IL_0029: brfalse.s    IL_003c
    IL_002b: ldarg.1      // arg
    IL_002c: box          !!0/*T*/
    IL_0031: isinst       [System.Runtime]System.Int32
    IL_0036: unbox.any    [System.Runtime]System.Int32

    // [22 17 - 22 23]
    IL_003b: pop

    // [27 9 - 31 11]
    IL_003c: ldarg.1      // arg
    IL_003d: box          !!0/*T*/
    IL_0042: isinst       [System.Runtime]System.Int32
    IL_0047: brfalse.s    IL_005b
    IL_0049: ldarg.1      // arg
    IL_004a: box          !!0/*T*/
    IL_004f: isinst       [System.Runtime]System.Int32
    IL_0054: unbox.any    [System.Runtime]System.Int32
posted @ 2025-08-14 20:40  Fallever  阅读(3)  评论(0)    收藏  举报