Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 哪种方式返回int更合适,即需要变量_C# - Fatal编程技术网

C# 哪种方式返回int更合适,即需要变量

C# 哪种方式返回int更合适,即需要变量,c#,C#,我仍然在学习C#,从VB.Net迁移过来,我一直想知道,在某些情况下,为什么我需要一个变量 给出这个片段 public int GetWaitTime(bool webProtocolError, int currentWait) { if (!webProtocolError) { if (currentWait < 16000) return currentWait + 250;

我仍然在学习C#,从VB.Net迁移过来,我一直想知道,在某些情况下,为什么我需要一个变量

给出这个片段

public int GetWaitTime(bool webProtocolError, int currentWait)
    {
       if (!webProtocolError)
        {
            if (currentWait < 16000)
                return currentWait + 250;
        }

        if (currentWait < 10000)
        {
            return 10000;
        }
        if (currentWait < 240000 && currentWait >=10000)
        {
            return currentWait*2;
        }
        return 240000;
    }
public int GetWaitTime(bool-webProtocolError,int-currentWait)
{
如果(!webProtocolError)
{
如果(当前等待<16000)
返回电流等待+250;
}
如果(当前等待<10000)
{
返回10000;
}
如果(currentWait<240000&¤tWait>=10000)
{
返回当前等待*2;
}
返回24万;
}
与此片段相对

public int GetWaitTime(bool webProtocolError, int currentWait)
    {
       var newWait = currentWait;
       if (!webProtocolError)
        {
            if (currentWait < 16000)
                newWait = currentWait + 250;
                return newWait;
        }
        if (currentWait < 10000)
        {
            newWait =10000;
            return newWait;
        }
        if (currentWait < 240000 && currentWait >=10000)
        {
            newWait = currentWait*2;
            return newWait;
        }
        return 240000;
  }
public int GetWaitTime(bool-webProtocolError,int-currentWait)
{
var newWait=currentWait;
如果(!webProtocolError)
{
如果(当前等待<16000)
newWait=currentWait+250;
返回newWait;
}
如果(当前等待<10000)
{
newWait=10000;
返回newWait;
}
如果(currentWait<240000&¤tWait>=10000)
{
newWait=currentWait*2;
返回newWait;
}
返回24万;
}

有什么真正的区别吗?VisualStudio将所有内容都视为int,因此我看不到任何类型问题。我很想听听专家们对哪种使用C语言最合适的反馈。

Q:有区别吗

A:是的,您在第二个示例中声明了一个额外的变量


Q:这会对最终编译的代码产生影响吗

A:可能是,也可能不是。例如,编译器可能决定在第一个代码段中创建一个临时的未命名变量,这将使这两个变量实际上编译为几乎相同的IL


Q:这是否会对运行时间、结果、准确性等产生影响

一点也不


前面的意见

Q:我应该使用哪一个作为“如何做事”的模板


A:对于这类代码,第一个。如果您在阅读代码时遇到问题,例如带有大量运动部件的大表达式和子表达式,请随意创建具有良好名称的新变量,以记录这些子表达式是什么、做什么和计算什么,但不要因为可以而仅仅引入变量。

Q:有区别吗

A:是的,您在第二个示例中声明了一个额外的变量


Q:这会对最终编译的代码产生影响吗

A:可能是,也可能不是。例如,编译器可能决定在第一个代码段中创建一个临时的未命名变量,这将使这两个变量实际上编译为几乎相同的IL


Q:这是否会对运行时间、结果、准确性等产生影响

一点也不


前面的意见

Q:我应该使用哪一个作为“如何做事”的模板


A:对于这类代码,第一个。如果您在阅读代码时遇到问题,例如带有大量运动部件的大表达式和子表达式,请随意创建具有良好名称的新变量,以记录这些子表达式是什么、做什么和计算什么,但不要因为可以而仅仅引入变量。

没有真正的区别,这里的主要问题是代码的样式。它应该易于遵循和维护

建议不多:

  • 尽量避免嵌套的if语句(如果在if中)。有时这并不像听起来那么容易,但VisualStudio必须帮助您

    if (!webProtocolError)
    {
        if (currentWait < 16000)
           return currentWait + 250;
    }
    
    if (!webProtocolError && currentWait < 16000)
    {
        return currentWait + 250;
    }
    
    if(!webProtocolError)
    {
    如果(当前等待<16000)
    返回电流等待+250;
    }
    如果(!webProtocolError&¤tWait<16000)
    {
    返回电流等待+250;
    }
    
  • 尽量避免使用幻数(如果合适的话)。使用类中描述的常量

    if (currentWait < 10000)
    {
        newWait =10000;
        return newWait;
    }
    
    private const int MinimumWait= 10000;
    if (currentWait < MinimumWait)
    {
        return MinimumWait;
    }
    
    if(当前等待<10000)
    {
    newWait=10000;
    返回newWait;
    }
    私有常量int MinimumWait=10000;
    if(当前等待<最小等待)
    {
    返回最小等待时间;
    }
    
  • 如果您有权存储函数的结果,请在体内调用它,并在方法末尾使用它返回

    public int GetWaitTime(bool webProtocolError, int currentWait)
    {
        var newWait = 240000;
        if (!webProtocolError)
        {
            if (currentWait < 16000)
                newWait = currentWait + 250;
        }
        if (currentWait < 10000)
        {
            newWait = 10000;
        }
        if (currentWait < 240000 && currentWait >=10000)
        {
            newWait = currentWait * 2;
        }
    
        return newWait;
    }
    
    public int GetWaitTime(bool-webProtocolError,int-currentWait)
    {
    var newWait=240000;
    如果(!webProtocolError)
    {
    如果(当前等待<16000)
    newWait=currentWait+250;
    }
    如果(当前等待<10000)
    {
    newWait=10000;
    }
    如果(currentWait<240000&¤tWait>=10000)
    {
    newWait=currentWait*2;
    }
    返回newWait;
    }
    

  • 这里没有真正的区别,主要是代码的风格。它应该易于遵循和维护

    建议不多:

  • 尽量避免嵌套的if语句(如果在if中)。有时这并不像听起来那么容易,但VisualStudio必须帮助您

    if (!webProtocolError)
    {
        if (currentWait < 16000)
           return currentWait + 250;
    }
    
    if (!webProtocolError && currentWait < 16000)
    {
        return currentWait + 250;
    }
    
    if(!webProtocolError)
    {
    如果(当前等待<16000)
    返回电流等待+250;
    }
    如果(!webProtocolError&¤tWait<16000)
    {
    返回电流等待+250;
    }
    
  • 尽量避免使用幻数(如果合适的话)。使用类中描述的常量

    if (currentWait < 10000)
    {
        newWait =10000;
        return newWait;
    }
    
    private const int MinimumWait= 10000;
    if (currentWait < MinimumWait)
    {
        return MinimumWait;
    }
    
    if(当前等待<10000)
    {
    newWait=10000;
    返回newWait;
    }
    私有常量int MinimumWait=10000;
    if(当前等待<最小等待)
    {
    返回最小等待时间;
    }
    
  • 如果您有权存储函数的结果,请在体内调用它,并在方法末尾使用它返回

    public int GetWaitTime(bool webProtocolError, int currentWait)
    {
        var newWait = 240000;
        if (!webProtocolError)
        {
            if (currentWait < 16000)
                newWait = currentWait + 250;
        }
        if (currentWait < 10000)
        {
            newWait = 10000;
        }
        if (currentWait < 240000 && currentWait >=10000)
        {
            newWait = currentWait * 2;
        }
    
        return newWait;
    }
    
    public int GetWaitTime(bool-webProtocolError,int-currentWait)
    {
    var newWait=240000;
    如果(!webProtocolError)
    {
    如果(当前等待<16000)
    newWait=currentWait+250;
    }
    如果(当前等待<10000)
    {