简体   繁体   English

如何在C#IF语句中使用此示例“ return”?

[英]How do I make use of this examples “return”, in a C# IF statement?

The sample I want to use is from This SO article . 我要使用的示例来自此SO文章

public static bool HasWritePermissionOnDir(string path)
{
    var writeAllow = false;
    var writeDeny = false;
    var accessControlList = Directory.GetAccessControl(path);
    if (accessControlList == null)
        return false;
    var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
    if (accessRules == null)
        return false;

    foreach (FileSystemAccessRule rule in accessRules)
    {
        if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) continue;

        if (rule.AccessControlType == AccessControlType.Allow)
            writeAllow = true;
        else if (rule.AccessControlType == AccessControlType.Deny)
            writeDeny = true;
    }

    return writeAllow && !writeDeny;
}

How can I use the "return writeAllow && !writeDeny" in an If statement? 如何在If语句中使用“ return writeAllow &&!writeDeny”? Or can I even do that. 或者我什至可以那样做。 Sorry if this is a dumb question, but in my limited experience I haven't see a return look like that. 抱歉,这是一个愚蠢的问题,但是根据我的经验有限,我还没有看到这样的回报。

Is this what you mean by "using it in an if-statement"?? 这是您在“ if语句中使用它”的意思吗?

if (HasWritePermissionOnDir(mypath))
{
    // Then write a new file to the directory
}

This code calls the function with mypath , and based on the returned result (which boils down to writeAllow && !writeDeny ), it may or may not evaluate to true and execute the if-statement. 这段代码使用mypath调用该函数,并根据返回的结果(归结为writeAllow && !writeDeny ),它可能会或可能不会评估为true并执行if语句。

At the end of the method, the boolean value is evaluated and returned in one line. 在该方法的末尾,将评估布尔值并在一行中返回。 But you could rewrite it like this (not recommended, just showing for clarity): 但是您可以这样重写它(不推荐,只是为了清楚起见):

bool ValueToReturn = WriteAllow && !writeDeny;
return ValueToReturn;

Or if you think about it in plain English: 或者,如果您用简单的英语来考虑:

Return true if write is allowed and not denied. 如果允许write 拒绝write则返回true。

The double ampersand ( & ) is a logical short-circuit meaning (in this case) that if the first term evaluates to false, it won't matter if the second term does or not, so it won't be evaluated. && )是一种逻辑短路含义(在这种情况下),即如果第一项的计算结果为false,则第二项的结果与否无关紧要,因此不会对其进行评估。

writeAllow && !writeDeny eavluates to a bool . writeAllow && !writeDeny变为bool The only requirement for an if statement is that whatever is inside the parentheses evaluates to a bool . if语句的唯一要求是,括号内的任何内容都必须为bool So using the expression inside of an if statement is as simple as: 因此,在if语句内部使用表达式非常简单:

if (writeAllow && !writeDeny)
{
    //do something
}

The && operator is a boolean operator, meaning it returns either true or false. &&运算符是布尔运算符,表示它返回true或false。 That means you can use this expression in an if statement exactly the way it is: 这意味着您可以完全按照以下方式在if语句中使用此表达式:

if (writeAllow && !writeDeny)

will work. 将工作。

If I understand, you want to take this code and put it inside one of your own functions. 据我了解,您想使用此代码并将其放在自己的函数之一中。 Is that correct? 那是对的吗?

Firstly, I suggest you leave it as a separate function. 首先,我建议您将其保留为单独的功能。 It makes it easier to discern when that piece of code is meant to do. 它使您更容易辨别该段代码的意图。 You can use it in an if statement with: 您可以在if语句中使用它:

if (HasWritePermissionOnDir("C:\\My\\Path"))
{
    // TODO
}

Or if you insist on copying the code inline: 或者,如果您坚持内联复制代码:

// TODO: Content of HasWritePermissionOnDir method without the return statement...
if (WriteAllow && !writeDeny)
{
    // TODO
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM