<namespace> 是 'type',在给定上下文中无效

<namespace> is a 'type', which is not valid in the given context

我讨厌检查布尔值以查看那里的状态,然后根据它切换它们,所以我写了一些东西来为我做这件事。

它一直给我以下错误: 是 'type',在给定的上下文中无效

这是命名空间 class:

namespace Bool
{
    public class ToggleState
    {

        static bool Toggle(bool Bool)
        {
            if (Bool == true)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

这是给出错误的代码

Test = Bool.ToggleState(Test);

这是因为您的命名空间被称为 "Bool",这使它成为一种类型。

我为您稍微重构了一下。

namespace Bool //Your namespace, it's a type
{
    public class ToggleState // Your class, also a type
    {
        static bool Toogle(bool boolValue) 
        {
            return !boolValue; //revert bool value and return it back
        }
    }
}

Name Bool(您当前的命名空间)很容易与 struct bool(变量类型)混淆。考虑给它起一个更有意义的名字。

尽管毫无疑问命名空间命名并不是最好的,但如果您修复了一些问题,我看不出有任何理由不工作:

1) 你应该 Test = Bool.ToggleState.Toggle(Test);

2) Toggle 方法应该是 public