C# 你如何检查一个方法是否返回true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8863967/
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 04:51:59  来源:igfitidea点击:

How do you check if a method has returned true

c#oop

提问by arcanine

Public bool SqlCheck(string username, string password) 
{
    // sql checks here 
    return true
} 

How do I check whether this has returned true or false in my main method? A code example would be helpful.

如何在我的主要方法中检查这是否返回 true 或 false?代码示例会有所帮助。

Does the boolean have a default value I should be aware of?

布尔值是否有我应该注意的默认值?

采纳答案by ChrisF

You simply have something like:

你只是有类似的东西:

bool result = SqlCheck(username, password);

if (result)
{
    // Worked!
}
else
{
    // Failed
}

If you don't need the result after the test you can simply have:

如果您在测试后不需要结果,您可以简单地拥有:

if (SqlCheck(username, password))
{
    // Worked!
}
else
{
    // Failed
}

booldefaults to false.

bool默认为false.

回答by ChrisF

As simple as this:

就这么简单:

if (SqlCheck(username, password))
{
    // SqlCheck returned true
}
else
{
    // SqlCheck returned false
}

回答by dknaack

Description

描述

The IFclause need a boolean (true or false) so your can do

IF条款需要一个布尔值(真或假),所以你可以做

MSDNThe if statement selects a statement for execution based on the value of a Boolean expression.

MSDNif 语句根据布尔表达式的值选择要执行的语句。

Sample

样本

 if (SqlCheck("UserName", "Password"))
 {
     // SqlCheck returns true
 }
 else 
 {
     // SqlCheck returns false
 } 


public bool SqlCheck(string username, string password) 
{
 // sql checks here 
    return true;
} 

If you need the result later you can save it to a variable.

如果以后需要结果,可以将其保存到变量中。

 bool sqlCheckResult= SqlCheck("UserName", "Password");
 if (sqlCheckResult)
 {
     // SqlCheck returns true
 }
 else 
 {
     // SqlCheck returns false
 } 

 // do something with your sqlCheckResult variable

More Information

更多信息

回答by Martin Bean

I'm not a C# programmer, but I'd imagine when you call this method in your mainmethod it will return the return value of SqlCheckwill it not?

我不是 C# 程序员,但我想当你在你的main方法中调用这个方法时,它会返回的返回值SqlCheck不是吗?

Pseudo-code:

伪代码:

public void function main()
{
    bool result = SqlCheck('martin', 'changeme');

    if (result == true) {
        // result was true
    } else {
        // result was false
    }
}

回答by dasblinkenlight

Boolean is a system type with two values understood by ifs, whiles, fors etc. of the .NET. You check for true value like this:

Boolean 是具有两个值的系统类型,.NET 的ifs, whiles, fors 等。您可以像这样检查真值:

if (SqlCheck(string username, string password) ) {
    // This will be executed only if the method returned true
}

The default value of boolvariables is false. This applies only to class/struct variables: local ones need to be initialized explicitly.

bool变量的默认值为false。这仅适用于类/结构变量:本地变量需要显式初始化。

回答by Ric

in your main method you can do this:

在您的主要方法中,您可以这样做:

bool sqlCheck = SqlCheck("username", "password");

if(sqlCheck) // ie it is true
{
    // do something
}

but your method is only returning true at the moment, I'm sure you will be doing other things in here to verify if the sql check is correct or not.

但是您的方法目前仅返回 true,我相信您会在这里做其他事情来验证 sql 检查是否正确。

回答by Fahim Parkar

bool myCheck = SqlCheck(myUser, myPassword);

OR

或者

bool myCheck = SqlCheck("user", "root");

Here user and root are actual strings to check....

这里 user 和 root 是要检查的实际字符串....

if (myCheck) {
    // succeeded
} else {
    //failed
}