c# - "Primary Constructor Body Is Not Allowed"错误

标签 c# error-handling

我的代码中出现错误“不允许使用主构造函数主体”,而且似乎找不到修复它的方法。错误发生是因为我创建了一个新的公共(public)方法,我也尝试使用私有(private)和 protected 方法,但错误仍然存​​在。这里还有其他人问了同样的问题 question .这个人得到的答案让我相信它可能是 OS X 特有的。 这是我的代码:

        string txt = WordBank ();

        string[] words = Moduel.TextToArray("Text.txt");
        string compWord = Moduel.Random (words);    
        Console.WriteLine ("I have chosen a random word, try to guess it one letter at a time");
    }

    public static void WordBank ();
    {
        string txt;

        Console.WriteLine ("Would you like to " +
            "(A) choose 4 letter words " +
            "(B) choose 5 letter words " +
            "(C) choose 6 letter words " +
            "(E) choose 7 lette r words or more?" +
            "(F) all words?");

        string input = Console.ReadLine ();
        if (input = "A")
            txt = "4 Letter Words.txt";
        else if (input = "B")
            txt = "5 Letter Words.txt";
        else if (input = "C")
            txt = "6 Letter Words.txt";
        else if (input = "E")
            txt = "7 Letters or More.txt";
        else if (input = "F")
            txt = "All Words.txt";
        else
        {
            Console.WriteLine("You haven't chosen a valid option, please try again");
            Main();
        }
        return txt;
    }
}

这是错误的图片。 Error Message.

最佳答案

错误在

public static void WordBank ();

只需从这一行中删除分号

public static void WordBank ()

并且您的函数返回了一个字符串值,因此将函数的定义更改为

public static string WordBank ()

关于c# - "Primary Constructor Body Is Not Allowed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34389117/

相关文章:

c# - 在同一台机器上运行 Selenium 驱动程序的多个实例

php - 如何在Symfony 4中使用dataTables防止错误 “Invalid JSON response”?

c# - 关键字 'event' 在 C# 中是可选的吗?

r - r glmnet错误: object cannot be coerced to type 'double'

asp.net-mvc-2 - 处理 DDD 风格的 ASP.Net MVC2 Web 应用程序中的错误

java - 如何在仍然具有连续循环的情况下避免堆栈溢出?

error-handling - AS400- MOVEA无法编译,无法将十进制数据结构移动到十进制数组

c# - websocket 连接失败?

c# - Migratordotnet 创建初始迁移

javascript - 尝试使用 connection.Start() 连接到服务器时出现 SignalR 客户端错误