我有一个包含在operationComboBox.Text中的字符串,我知道该字符串将是“+”或“ - ”。然后我就可以使用此代码执行2个方程之间的加减运算:我可以使用字符串值在C#计算中替换+或 - 运算符吗?
if ((operationComboBox.Text == "-"))
{
equation3XCoeff = equations[index1].XCoeff - equations[index2].XCoeff;
equation3YCoeff = equations[index1].YCoeff - equations[index2].YCoeff;
equation3Answer = equations[index1].Answer - equations[index2].Answer;
}
else //if (operationComboBox.Text=="+")
{
equation3XCoeff = equations[index1].XCoeff + equations[index2].XCoeff;
equation3YCoeff = equations[index1].YCoeff + equations[index2].YCoeff;
equation3Answer = equations[index1].Answer + equations[index2].Answer;
}
我的问题是,我可以摆脱if语句,并直接在资金使用字符串值来进行,以缩短我的代码如何?它可能不是太重要,但我只是想我的代码很短,三个计算几乎是重复的,但对于标志。
可能重复:http://stackoverflow.com/questions/13522693/c-sharp-convert-string-to-operator –