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

标签 c# namespaces syntax-error

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

它不断给我以下错误:
是一个“类型”,在给定的上下文中无效

这是命名空间和类:

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(变量类型)混淆。考虑给它更有意义的名字。

关于c# - <namespace> 是 'type' ,在给定的上下文中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39963369/

相关文章:

c# - 从 GAC 或其他地方浏览/选择程序集

c# - 解释 C# 中的语句

XML 模式 - 命名空间选择歧义

reactjs - Gulp + Browserify + Babelify意外 token

c++ - 表达式必须是struct的可修改左值

c# - 使用 EF 查询映射 sql 查询不起作用

c# - 如何在 GSMComm 库中连接长短信?

包含 jQuery 的 JavaScript 命名空间

C++ extern 关键字和全局变量

python 3 : for loop syntax with dictionary