C# “类、结构或接口成员声明中的无效标记 '{'”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11062933/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:20:31  来源:igfitidea点击:

"Invalid token '{' in class, struct, or interface member declaration"

c#token

提问by Anees Kalsekar

I get the error "Invalid token '{' in class, struct, or interface member declaration" in my following C # code.

我在以下 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)
        {   

        }
   }
}

Your help in solving will be greatly appreciated.

将不胜感激您对解决问题的帮助。

Thank you.

谢谢你。

Anees

安妮

回答by Zbigniew

There is error because you have semicolon in there and there is wrong variable name, fix:

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

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

回答by Nikhil Agrawal

Remove semicolon after your method declaration void add(int x, int y);. Change it to

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

void add(int x, int y)

and What is charinside Console.WriteLine("addition is " + char)? Change charto c.

char里面是什么Console.WriteLine("addition is " + char)?更改charc

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

No need to write c.ToString()because when an entity is appended with string it automatically calls its ToString()method. (Coz this method is available to all entities of .Net).

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

回答by Md Kamruzzaman Sarker

Remove semicolon after this method declaration and change char to c in Console.Writeline

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

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

Instead write

而是写

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