简体   繁体   English

更有效地删除使用IF语句C#

[英]More efficient abridged use of IF statement C#

I would like to know if there is a better more efficient way to use if statements rather than just long lines of 我想知道是否有更好的方法来使用if语句而不仅仅是长行

if(){
    //code
}else if(){
    //code
} else{
    //code
}

I've done some research on the site and I found this: 我在网站上做了一些研究,我发现了这个:

If you have only 2 values, I strongly suggest to use the code you posted, because is likely the most readable, elegant and fast code possible (IMHO). 如果你只有2个值,我强烈建议使用你发布的代码,因为它可能是最易读,优雅和快速的代码(恕我直言)。

But if you have more cases like that and more complicated, you could think to use a switch statement: 但是如果你有更多这样的案例并且更复杂,你可以考虑使用switch语句:

switch (el.type)
{
    case ElementType.Type1:
    case ElementType.Type2:
    case ElementType.Type3:
        //code here
        break;
    case ElementType.Type4:
    case ElementType.Type5:
        //code here
        break;
    case ElementType.Type6:
        //code here
        break;
}
that translated in if statements would be:

if (el.type == ElementType.Type1 ||
    el.type == ElementType.Type2 ||
    el.type == ElementType.Type3 )
{
    // code here
}else if(el.type == ElementType.Type4 ||
         el.type == ElementType.Type5)
{
    // code here
}else if(el.type == ElementType.Type6)
{
    // code here
}

They're perfectly equal to me, but the switch seems more readable/clearer, and you need to type less (ie it's "shorter" in term of code length) :) 它们完全等同于我,但是开关看起来更可读/更清晰,你需要输入更少(即代码长度方面的“更短”):)

Although I don't quite understand what it is telling me, is it saying that a switch statement is a better use for long if statements or? 虽然我不太明白它告诉我的是什么,但它是否说switch语句更适用于long if语句或?

To have some context surrounding my problem, I have a Windows Forms application with some radio buttons - A questionnaire if you will - I want to know if there is more efficient ways that reduces repetitive unnecessary lines of code and replaces them with short code that does the same job. 为了有一些关于我的问题的背景,我有一个带有一些单选按钮的Windows窗体应用程序 - 如果你愿意,我会问一个调查问卷 - 我想知道是否有更有效的方法可以减少重复的不必要的代码行并用短代码替换它们同样的工作。

Switch statements as well as many if-else statements are often a sign of the smelly design. 切换语句以及许多if-else语句通常是臭设计的标志。 Consider refactoring into a more object-oriented design 考虑重构为更面向对象的设计

If-else not much more readable than switch-case and vice-versa. If-elseswitch-case更不可读,反之亦然。

The goal should be more readable / clearer code, which does not always mean the shortest statement. 目标应该是更可读/更清晰的代码,这并不总是意味着最短的陈述。

If you have very complex logic, with lots of nested if / else branches, try to split it in smaller, simpler routines. 如果你有非常复杂的逻辑,有很多嵌套的if / else分支,试着把它拆分成更小,更简单的例程。

You could apply CoR pattern . 您可以应用CoR模式 Here you will fine one of .NET implementations. 在这里,您将完成.NET实现之一。 When you combine this with IoC container you may achieve very flexible architecture... 当您将其与IoC容器结合使用时,您可以实现非常灵活的架构......

switch statements are not only easier to read, they are easier for the compiler to optimize because they explicitly specify one value you are triggering all the code off of. switch语句不仅更容易阅读,而且编译器更容易优化,因为它们明确指定了一个触发所有代码的值。 The compiler can easily optimize this using a hash or a jump table rather than a sequence of comparisons, making it much faster (when it can do so). 编译器可以使用散列或跳转表而不是一系列比较来轻松优化它,使其更快(当它可以这样做时)。 Technically, it could detect that the if statements are the same thing, but may or may not be sophisticated enough to do so. 从技术上讲,它可以检测到if语句是相同的,但可能会或可能不够复杂。 So, if you have 256 separate cases triggering off the value of a byte , a switch can be compiled into a hard coded array of code offsets (each entry in the array is the offset of the code handling that case), and no comparison is done at all because it can just use the byte value as an index into the hard coded array and jump to the correct code. 因此,如果你有256个单独的情况触发一个byte的值,可以将一个开关编译成代码偏移的硬编码数组(数组中的每个条目都是处理该情况的代码的偏移量),并且没有比较是完成所有因为它只能使用字节值作为硬编码数组的索引并跳转到正确的代码。 This is much faster than doing 255 (or 256) compares. 这比做255(或256)比较快得多。

Switches using string values are also handled specially, using hash codes for better performance. 使用string值的开关也是专门处理的,使用哈希码可以获得更好的性能。 You may be able to do something similar explicitly, but your code will be much less readable. 您可以明确地执行类似的操作,但是您的代码可读性会低得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM