C# 名称为;";在当前上下文中不存在

C# 名称为;";在当前上下文中不存在,c#,C#,我的程序不断出现错误“当前上下文中不存在名称“ok”。我做错了什么 namespace Game { class Program { static bool SelfTest() { bool ok = GameModel.SelfTest(); if (ok) System.Diagnostics.Debug.WriteLine("Succeded");

我的程序不断出现错误“当前上下文中不存在名称“ok”。我做错了什么

namespace Game
{
    class Program
    {
        static bool SelfTest()
        {
            bool ok = GameModel.SelfTest();

            if (ok)
                System.Diagnostics.Debug.WriteLine("Succeded");
            else 
                System.Diagnostics.Debug.WriteLine("Failed");

            return ok;
        }

        static void Main(string[] args)
        {
            bool ok = SelfTest();
        }
    }
}

namespace Game
{
    class GameModel
    {
        public static bool SelfTest()
        {
            ok = true;

            return ok;
        }
    }
}

您尚未在此处声明
ok

public static bool SelfTest()
{
    ok = true; // should be bool ok = true;


    return ok;
}
改变

或者只是

public static bool SelfTest()
{
    return true;
}

Game.Program.SelfTest()
Game.Program.Main
中,您只清除了布尔值
ok
,但在
Game.GameModel.SelfTest()中没有清除
请改用以下代码(或者您可能只想使用
return true
):


GameModel
类中,您引用的是一个未声明的变量

要解决此问题,您只需更改以下内容:

public static bool SelfTest()
{
    ok = true;
    return ok;
}
为此:

public static bool SelfTest()
{
    bool ok = true;
    return ok;
}

因为名字“ok”在当前上下文中的
GameModel.SelfTest
中不存在。下次,双击VisualStudio中的错误,它会将您带到可能存在错误的位置。问:我做错了什么?答:甚至连基本的文档都忽略了。阅读有关C#中的变量声明的内容,所以它不是一个编译器。。。阅读错误消息应该会指出问题所在。可能重复的
namespace Game
 {
   class GameModel
   {

    public static bool SelfTest()
    {
        bool ok = true;


        return ok;
    }
  }
}
public static bool SelfTest()
{
    ok = true;
    return ok;
}
public static bool SelfTest()
{
    bool ok = true;
    return ok;
}