Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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# 有没有更好的方法来检查布尔逻辑_C#_Xml_Boolean - Fatal编程技术网

C# 有没有更好的方法来检查布尔逻辑

C# 有没有更好的方法来检查布尔逻辑,c#,xml,boolean,C#,Xml,Boolean,我有一个这样的xml文件 <Config> <Allowed></Allowed> </Config> isAllowed应在 标签不存在 是存在的,但是是空的 具有除true、false、yes或no以外的任何其他值 以下是执行此操作的代码: if (isAllowed == null) { DoSomething(); return true; } if (isAllowed.Length == 0) { Do

我有一个这样的xml文件

<Config>
    <Allowed></Allowed>
</Config>
isAllowed应在

  • 标签不存在
  • 是存在的,但是是空的
  • 具有除true、false、yes或no以外的任何其他值
  • 以下是执行此操作的代码:

    if (isAllowed == null)
    {
        DoSomething();
        return true;
    }
    if (isAllowed.Length == 0)
    {
        DoSomething();
        return true;
    }
    if (isAllowed.Length != 0)
    {
        if (isAllowed.ToUpper() != "FALSE" && isAllowed.ToUpper() != "NO")
        {
            DoSomething();
            return true;
        }
    }
    
    一定有更好的办法吗

    if (isAllowed == null)
    {
        DoSomething();
        return true;
    }
    if (isAllowed.Length == 0)
    {
        DoSomething();
        return true;
    }
    
    可替换为:

    if (string.IsNullOrEmpty(isAllowed)
    {
        DoSomething();
        Return true;
    }
    
    但实际上,根据您的标准,我认为
    string.IsNullOrWhiteSpace(isAllowed)
    更合适,因为如果标记的内容为“空”,它将返回true

    此外,您不需要第二次满足以下条件,因为如果第一次满足该条件,函数将返回(短路评估)。这意味着您当前在第二个
    If
    块中的语句无论如何都不会执行

    if (isAllowed.Length != 0)
    
    我第一个本能的反应是把这件事弄得更干净,就像乔恩在回答中所说的那样,重复这件事没有任何好处。不过,我认为这是另一个好的设计,如果你介绍更多的条件,它会更干净:

    private static bool Validate(string isAllowed)
    {
        var defaultTrueConditions = new[] {"true", "false", "yes", "no"};
        if (string.IsNullOrWhiteSpace(isAllowed) ||
            defaultTrueConditions.Contains(isAllowed, StringComparer.OrdinalIgnoreCase))
        {
            DoSomething();
            return true;
        }
        return false;
    }
    
    可替换为:

    if (string.IsNullOrEmpty(isAllowed)
    {
        DoSomething();
        Return true;
    }
    
    但实际上,根据您的标准,我认为
    string.IsNullOrWhiteSpace(isAllowed)
    更合适,因为如果标记的内容为“空”,它将返回true

    此外,您不需要第二次满足以下条件,因为如果第一次满足该条件,函数将返回(短路评估)。这意味着您当前在第二个
    If
    块中的语句无论如何都不会执行

    if (isAllowed.Length != 0)
    
    我第一个本能的反应是把这件事弄得更干净,就像乔恩在回答中所说的那样,重复这件事没有任何好处。不过,我认为这是另一个好的设计,如果你介绍更多的条件,它会更干净:

    private static bool Validate(string isAllowed)
    {
        var defaultTrueConditions = new[] {"true", "false", "yes", "no"};
        if (string.IsNullOrWhiteSpace(isAllowed) ||
            defaultTrueConditions.Contains(isAllowed, StringComparer.OrdinalIgnoreCase))
        {
            DoSomething();
            return true;
        }
        return false;
    }
    

    听起来你最好是这样:

    string isAllowed = (string)xml.Root
                                  .Element("Config")
                                  .Elements("Allowed")
                                  .SingleOrDefault();
    
    // This deals with the nullity aspect. (The "Yes" is just for clarity - it could
    // be any value other than "No" or "False" in some form.)
    isAllowed = isAllowed ?? "Yes";
    
    bool isFalse = isAllowed.Equals("No", StringComparison.OrdinalIgnoreCase) ||
                   isAllowed.Equals("False", StringComparison.OrdinalIgnoreCase);
    
    return !isFalse;
    
    基本上,您默认为
    true
    这一事实意味着,如果您找到一个元素,并且该元素的值为
    No
    false
    ,则返回值仅应为
    false
    ,不区分大小写。请注意,我在这里使用了顺序匹配-您可能想更改它,例如,更改为
    CurrentCultureInoRecase
    InvariantCultureInoRecase

    现在还不清楚DoSomething方法调用是从哪里来的,但我还是要把它分开。编写一个只确定适当值的方法,如上所示-然后:

    bool allowed = CheckAllowed(doc);
    if (allowed)
    {
        DoSomething();
    }
    // And use allowed here too
    

    对我来说,这是一个更清晰的分离。

    听起来你最好这样:

    string isAllowed = (string)xml.Root
                                  .Element("Config")
                                  .Elements("Allowed")
                                  .SingleOrDefault();
    
    // This deals with the nullity aspect. (The "Yes" is just for clarity - it could
    // be any value other than "No" or "False" in some form.)
    isAllowed = isAllowed ?? "Yes";
    
    bool isFalse = isAllowed.Equals("No", StringComparison.OrdinalIgnoreCase) ||
                   isAllowed.Equals("False", StringComparison.OrdinalIgnoreCase);
    
    return !isFalse;
    
    基本上,您默认为
    true
    这一事实意味着,如果您找到一个元素,并且该元素的值为
    No
    false
    ,则返回值仅应为
    false
    ,不区分大小写。请注意,我在这里使用了顺序匹配-您可能想更改它,例如,更改为
    CurrentCultureInoRecase
    InvariantCultureInoRecase

    现在还不清楚DoSomething方法调用是从哪里来的,但我还是要把它分开。编写一个只确定适当值的方法,如上所示-然后:

    bool allowed = CheckAllowed(doc);
    if (allowed)
    {
        DoSomething();
    }
    // And use allowed here too
    

    对我来说,这是一个更清晰的分离。

    你不能将IsAllowed视为布尔值吗?如果IsAllowed是布尔值,我将如何检查'true''false''yes''no'?如果IsAllowed是布尔值,你不能将IsAllowed视为布尔值吗?如果IsAllowed是布尔值,我将如何检查'true''false''yes''no''?我想他不知道“else”块:pNesting将引入更多的复杂性,我想你知道;)您的最终代码不正确。它将执行
    DoSomething
    ,即使是
    的“false”
    的“no”
    ,而OP的代码没有执行。另外,如果您担心空白,那么您应该在比较之前修剪输入。我想他也不知道“else”块:pNesting会引入更多的复杂性,我想您知道;)您的最终代码不正确。它将执行
    DoSomething
    ,即使是
    的“false”
    的“no”
    ,而OP的代码没有执行。另外,如果您担心空白,那么您应该在比较之前对输入进行修剪。对于我想做的事情,请欣赏。对于中间位,为什么不
    new[]{“No”,“False”}.Contains(isAllowed,StringComparer.OrdinalIgnoreCase)
    @Ani:这也是一个选项-无论你觉得哪个最具可读性。在我想做的事情中,请欣赏它。对于中间的部分,为什么不
    new[]{“No”,“False”}.Contains(isAllowed,StringComparer.OrdinalIgnoreCase)
    @Ani:这也是一个选项-无论你觉得最具可读性。