C# 返回布尔值的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12624666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:55:45  来源:igfitidea点击:

Method with a bool return

c#if-statementreturn

提问by Parys Bernard

I was making a method with a boolreturn value and I had a problem:

我正在制作一个带有bool返回值的方法,但遇到了一个问题:

This works

这有效

private bool CheckAll()
{
  //Do stuff
  return true;
}

But this dosn't, the method can't detect a return value if it's in a IF-statement.

但这不是,如果返回值在 IF 语句中,则该方法无法检测到返回值。

private bool CheckAll()
{
  if (...)
  {
    return true;
  }
}

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Nadir Sampaoli

private bool CheckAll()
{
    if ( ....)
    {
        return true;
    }

    return false;
}

When the if-condition is false the method doesn't know what value should be returned (you probably get an error like "not all paths return a value").

当 if 条件为 false 时,该方法不知道应该返回什么值(您可能会收到类似“并非所有路径都返回值”之类的错误)。

As CQQL pointed outif you mean to return true when your if-condition is true you could have simply written:

正如CQQL 指出的那样,如果您打算在 if 条件为真时返回真,您可以简单地编写:

private bool CheckAll()
{
    return (your_condition);
}

If you have side effects, and you want to handle them before you return, the first (long) version would be required.

如果您有副作用,并且想在返回之前处理它们,则需要第一个(长)版本。

回答by Marten

Long version:

长版:

private bool booleanMethod () {
    if (your_condition) {
        return true;
    } else {
        return false;
    }
}

But since you are using the outcome of your condition as the result of the method you can shorten it to

但是由于您使用条件的结果作为方法的结果,您可以将其缩短为

private bool booleanMethod () {
    return your_condition;
}

回答by waqas aziz

     public bool roomSelected()
    {
        int a = 0;
        foreach (RadioButton rb in GroupBox1.Controls)
        {
            if (rb.Checked == true)
            {
                a = 1;
            }
        }
        if (a == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

this how I solved my problem

这就是我如何解决我的问题

回答by Dogg Bookins

Use this code:

使用此代码:

public bool roomSelected()
{
    foreach (RadioButton rb in GroupBox1.Controls)
    {
        if (rb.Checked == true)
        {
            return true;
        }
    }
    return false;
}

回答by albaiti

this is how you will solve it

这就是你将如何解决它

    public  bool checkinputs(int a, int b){
        bool condition = true;
        if (a > b) {

            condition = false;
        } else if (a == b) {

            condition = false;
        } else {
            condition = true;
        }

            return condition;

    }

回答by Md. Hasan Uzzaman

You are missing the else portion. If all the conditions are false then else will work where you haven't declared and returned anything from else branch.

您缺少 else 部分。如果所有条件都为假,则 else 将在您尚未从 else 分支声明和返回任何内容的地方工作。

private bool CheckALl()
{
  if(condition)
  {
    return true
  }
  else
  {
    return false
  }
}