C# 非常简单的代码中的类、结构或接口成员声明中的无效标记“while”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/397019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid token 'while' in class, struct, or interface member declaration in very simple code
提问by Eric
I am not sure what the problem is but I keep receiving this error when I try to use a while statement in my code.
我不确定问题是什么,但是当我尝试在代码中使用 while 语句时,我不断收到此错误。
Invalid token 'while' in class, struct, or interface member declaration
类、结构或接口成员声明中的无效标记“while”
I want to use a while loop to have something continuously update while a statement is true.
我想使用 while 循环在语句为真时不断更新某些内容。
The rest of my code is rather long but whenever I type in the syntax:
我的其余代码相当长,但每当我输入语法时:
while(a<b)
{
//do whatever i want it to do here
}
It gives me that compiler error right off the bat. Not quite sure what the problem is. I am doing this in a C# windows application under the Form1.cs file with all the other event handlers (for buttons and such). Thanks!
它立即给了我编译器错误。不太确定是什么问题。我在 Form1.cs 文件下的 C# windows 应用程序中使用所有其他事件处理程序(用于按钮等)执行此操作。谢谢!
I was unaware that loops had to be placed within a method (fairly new to c#), but I tried it and no errors were returned. Thanks for your help everybody!
我不知道循环必须放在一个方法中(对 c# 来说相当新),但我尝试了它并且没有返回任何错误。谢谢大家的帮助!
Previously, I just had the loop within the main class of the program.
以前,我只是在程序的主类中有循环。
采纳答案by JaredPar
Based on the error, it sounds like the compiler thinks this code is typed directly in the body of a class/struct/interface declaration. Statements while/if/for/etc ... must appear with in a method.
根据错误,听起来编译器认为此代码是直接在类/结构/接口声明的主体中键入的。语句 while/if/for/etc ... 必须出现在方法中。
Try moving this code into a method to fix the problem. If it's in a method, you likely have a mismatched brace problem.
尝试将此代码移动到解决问题的方法中。如果它在一个方法中,你可能有一个不匹配的括号问题。
回答by stu
There's nothing wrong with the while, it's something above it that's the problem. Check for mismatched braces and semicolons in a comment or something like that.
这段时间没有任何问题,问题在于它上面的东西。检查注释或类似内容中是否有不匹配的大括号和分号。
回答by Ali khan
C# is not allowed to write code directly into the classes; it is allowed to write only data members and function members directly into the classes.
C# 不允许直接在类中编写代码;只允许将数据成员和函数成员直接写入类。
回答by NealWalters
You can also get this if you have punctuation issues, I got it today when I missing a simple parentheses:
如果您有标点符号问题,您也可以得到这个,我今天在缺少一个简单的括号时得到了它:
public static string GetPasswordForEnvironment)
should have been:
本来应该:
public static string GetPasswordForEnvironment()
But the error showed up on the first "if" statement later in the function.
但是错误出现在函数后面的第一个“if”语句中。
回答by santhu Style
C# does not provide writing the for, while or foreach kind of looping statement directly inside the class. These statements should be within a user defined method, main method or constructors.
C# 不提供直接在类内部编写 for、while 或 foreach 类型的循环语句。这些语句应该在用户定义的方法、主方法或构造函数中。
class Array
{
public static void main(string[] args)
{
int[] n = new int[10]; /* n is an array of 10 integers */
/* initialize elements of array n */
for (int i = 0; i < 10; i++)
{
n[i] = i + 100;
}
/* output each array element's value */
foreach (int j in n)
{
int i = j - 100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
}
}