如何替换太多if .. else [英] How to replace too many if.. else

查看:315
本文介绍了如何替换太多if .. else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个方法,如果用于检查null条件太多,不能用switch case替换,而且还有太多if else ..所以它的显示方法在Ndepend中过于复杂的警告。如何解决这个问题?

In my project I have a method where there are too many if for checking null condition which can''t be replaced by switch case and also there are too many if else.. So its showing method too complex warning in Ndepend.. how to fix this?

推荐答案

如果您只是检查空值,可以随时使用以下运算符:



If you are simply checking for nulls you can always use the following operator:

int myLocal = myParameter ?? 0;
string myString = myStringParam ?? "default value";





它将检查第一部分为null,如果为null则返回第二个值。



It will check the first part for null, and if it is null return the second value.


参考下面的链接..

hope这将对你有所帮助



链接1 [ ^ ]
Refer below link ..
hope it will help full for you

Link 1[^]


首先你必须知道:

1:如果(缩进程度增加)

2: If-else(小于If)

3:elseif(远低于2)



如果执行时间:

1:如果{即使条件为真,如果是,它会检查所有,如果,由你使用}

2:If-else {相同但压痕水平更少}

3:Else-if {Best bcoz如果条件满足第一个if or else-if,剩余w生病不执行。如果不是第一次,那么就是第二次或第三次等等。有点像休息}



所以最后你必须使用Else-if.Or For更好的信息见本书第3课 - {让我们C} by-Yashwant Kanetkar
First u have to know:
1:If (Level of indentation increased)
2:If-else (Less than If)
3:elseif (Much less than above two)

In case of Execution time:
1:If {Even if the condition true by 1st if,it will check to all if,used by u}
2:If-else{Same above but level of indentation is less}
3:Else-if {Best bcoz if condition satisfied by 1st if or else-if,remaining will not execute.if not by 1st then 2nd or 3rd and so on.Just little bit like break}

so finally u have to use Else-if.Or For much better information see Lesson 3 of the book-{Let us C}by-Yashwant Kanetkar


这篇关于如何替换太多if .. else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆