我有一个非常快速的问题关于使用两个变量的最佳方式。基本上我有一个枚举和一个int,我想在几个ifs中获得的值。我应该把它们声明外,如果是或内 - 考虑下面的例子:c#这是更好的方式来声明两个参数
e.g.a:
public void test() {
EnumName? value = null;
int distance = 0;
if(anotherValue == something) {
distance = 10;
value = getValue(distance);
}
else if(anotherValue == somethingElse) {
distance = 20;
value = getValue(distance);
}
if (value == theValueWeWant){
//Do something
}
OR
e.g.2
public void test() {
if(anotherValue == something) {
int distance = 10;
EnumType value = getValue(distance);
if (value == theValueWeWant){
//Do something
}
else if(anotherValue == somethingElse) {
int distance = 20;
EnumType value = getValue(distance);
if (value == theValueWeWant){
//Do something
}
}
我只是好奇,这是最好的?或者如果有更好的方法?
都是不同的场景的第一个例子需要如果在第二条语句之外的变量例如,if语句范围之外不需要变量 – Zaki