小编Eri*_*ron的帖子

codeDom中的switch语句(跳转表样式)

我知道switch语句不可用CodeDom以及编译器如何处理switch语句.

因此,出于性能原因,当存在许多情况时,我不想使用If-else
为什么switch语句而不是if-else?

是否可以生成代码来模拟给定案例列表的Jump表.

switch(value) {
    case 0: return Method0();
    case 1: return Method1();
    case 4; return Method4();
}
Run Code Online (Sandbox Code Playgroud)

会产生:

    private delegate object Method();

    Method[] _jumpTable = new Method[] { Method0, Method1, null, null, Method4 };

    private object GetValue(int value)
    {
        if (value < 0 || value > 4) 
            return null;
        return _jumpTable[value]();
    }
Run Code Online (Sandbox Code Playgroud)

如果序列中有漏洞或列表稀疏,分析案例列表并生成数组的最佳方法是什么?

c# codedom switch-statement

5
推荐指数
1
解决办法
1252
查看次数

标签 统计

c# ×1

codedom ×1

switch-statement ×1