小编Max*_*eev的帖子

将"if"语句条件移动到局部变量会使C#编译器不满意

能否请您解释以下情况的原因.

今天我编写了代码(只更改了变量名称):

private void Foo() 
{
    int firstInteger, secondInteger;
    const string firstStringValue = "1", secondStringValue = "2";

    if (!string.IsNullOrWhiteSpace(firstStringValue) && int.TryParse(firstStringValue, out firstInteger) &&
        !string.IsNullOrWhiteSpace(secondStringValue) && int.TryParse(secondStringValue, out secondInteger))
    {
        // Using firstInteger and secondInteger here
        firstInteger++;
        secondInteger++;
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,直到我决定将if条件移动到变量:

private void Foo()
{
    int firstInteger, secondInteger;
    const string firstStringValue = "1", secondStringValue = "2";

    bool firstIntegerAndSecondIntegerAreSpecified = 
        !string.IsNullOrWhiteSpace(firstStringValue) && int.TryParse(firstStringValue, out firstInteger) &&
        !string.IsNullOrWhiteSpace(secondStringValue) && int.TryParse(secondStringValue, out secondInteger);

    if (firstIntegerAndSecondIntegerAreSpecified)
    {
        // Use firstInteger and secondInteger …
Run Code Online (Sandbox Code Playgroud)

c# compiler-construction

7
推荐指数
2
解决办法
607
查看次数

标签 统计

c# ×1

compiler-construction ×1