【问题标题】:"Invalid token '{' in class, struct, or interface member declaration" [closed]“类、结构或接口成员声明中的无效标记 '{'”[关闭]
【发布时间】:2012-06-19 05:56:06
【问题描述】:

我在以下 C# 代码中收到错误“类、结构或接口成员声明中的标记无效”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static int a, b;
        void add(int x, int y);
        {
            int c= x+y;
            Console.WriteLine("addition is " + char);
        }

        static void Main(string[] args)
        {   

        }
   }
}

我们将不胜感激您的帮助。

谢谢。

阿内斯

【问题讨论】:

    标签: c# token


    【解决方案1】:

    删除此方法声明后的分号,并在 Console.Writeline 中将 char 更改为 c

    void add(int x, int y);
    {
            int c= x+y;
            Console.WriteLine("addition is " + char);
        }
    

    改为写

     void add(int x, int y)
    {
            int c= x+y;
            Console.WriteLine("addition is " + c);
        }
    

    【讨论】:

    • 这简直难以置信!!!我有很多荒谬的错误,只有在我阅读了这篇评论并搜索了神秘的分号后,我才找到了一个,它清除了我所有的错误!你救了我! VisualStudio 可以更好地处理它...:)
    【解决方案2】:

    删除方法声明 void add(int x, int y); 后的分号。改成

    void add(int x, int y)
    

    Console.WriteLine("addition is " + char) 中的 char 是什么?将char 更改为c

    Console.WriteLine("addition is " + c);
    

    无需编写c.ToString(),因为当实体附加字符串时,它会自动调用其ToString() 方法。 (因为此方法适用于 .Net 的所有实体)。

    【讨论】:

    • 非常感谢您的时间和帮助。
    【解决方案3】:

    有错误,因为你有分号并且变量名错误,修复:

    void add(int x, int y)
    {
        int c= x+y;
        Console.WriteLine("addition is " + c);
    }
    

    【讨论】:

    • 帮助很大......非常感谢......现在工作正常。
    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 2014-06-22
    • 2016-07-10
    • 1970-01-01
    相关资源
    最近更新 更多