为什么在我的C# .NET 7脚本中,VS Code中的分号是红色的?

huangapple go评论102阅读模式
英文:

Why semicolon is red in my c# dotnet 7 script in vscode?

问题

  1. [Screenshot of error message in VSCode](https://i.stack.imgur.com/5Kbzn.png)
  2. ```csharp
  3. using System;
  4. namespace CodeWarTests
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.WriteLine("Hello World!");
  11. private int test = 0;
  12. }
  13. }
  14. }

I'm expecting not red semicolon.

  1. <details>
  2. <summary>英文:</summary>
  3. [Screenshot of error message in VSCode](https://i.stack.imgur.com/5Kbzn.png)
  4. ```csharp
  5. using System;
  6. namespace CodeWarTests
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine(&quot;Hello World!&quot;);
  13. private int test = 0;
  14. }
  15. }
  16. }

I'm expecting not red semicolon.

答案1

得分: 3

The private qualifier is for members of the class, not for local variables. It shows that it therefore expected a } after the ; in your line 10.

因此,以下版本不应显示任何错误:

  1. using System;
  2. namespace CodeWarTests
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Console.WriteLine("Hello World!");
  9. int test = 0;
  10. }
  11. }
  12. }
英文:

The private qualifier is for members of the class, not for local variables. It shows that it therefore expected a } after the ; in your line 10.

https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1513

Therefore the following version should not show any error:

  1. using System;
  2. namespace CodeWarTests
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Console.WriteLine(&quot;Hello World!&quot;);
  9. int test = 0;
  10. }
  11. }
  12. }

huangapple
  • 本文由 发表于 2023年6月29日 23:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定