C# 编译时出错:“预期的类、委托、枚举、接口或结构”

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

Errors when compiling: "Expected class, delegate, enum, interface, or struct"

c#compiler-errors

提问by Cookie Monster

What is wrong with this code? This program is meant to copy a file and email it to a email address, but it doesn't.

这段代码有什么问题?该程序旨在复制文件并将其通过电子邮件发送到电子邮件地址,但事实并非如此。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }

    public void email_send()
   {
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your [email protected]");
    mail.To.Add("[email protected]");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}
}

This shows the following compiler errors:

这显示了以下编译器错误:

  1. Expected class, delegate, enum, interface, or struct
  2. Expected class, delegate, enum, interface, or struct
  3. Expected class, delegate, enum, interface, or struct
  4. Expected class, delegate, enum, interface, or struct
  5. Expected class, delegate, enum, interface, or struct
  6. Expected class, delegate, enum, interface, or struct
  7. Type or namespace definition, or end-of-file expected Expected class, delegate, enum, interface, or struct
  1. 预期的类、委托、枚举、接口或结构
  2. 预期的类、委托、枚举、接口或结构
  3. 预期的类、委托、枚举、接口或结构
  4. 预期的类、委托、枚举、接口或结构
  5. 预期的类、委托、枚举、接口或结构
  6. 预期的类、委托、枚举、接口或结构
  7. 类型或命名空间定义,或文件结束预期 预期的类、委托、枚举、接口或结构

What can I do about this?

我该怎么办?

回答by Bearcat9425

You Method is outside the class for one thing. Copy it into the form 1 class and it should clear up any intellisense issues

你的方法是在课堂之外的一件事。将其复制到表单 1 类中,它应该清除任何智能感知问题

回答by ianaldo21

email_send method is not defined within a class.

email_send 方法未在类中定义。

回答by Joel Coehoorn

The email_send()method is outside of the class declaration. It's still inside the namespace, but you must also place it inside the class. Additionally, at no point is the method ever called. It's dead code.

email_send()方法在类声明之外。它仍然在命名空间内,但您也必须将它放在类内。此外,该方法从未被调用过。这是死代码。

Move the method inside your class definition and then call the method from inside Form_Load()

在类定义中移动该方法,然后从 Form_Load() 内部调用该方法

回答by Evan L

Exactly what everyone else is saying, but cut/paste this and you should correct the errors:

正是其他人所说的,但剪切/粘贴这个,你应该纠正错误:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.
using System.Net;
using System.Net.Mail;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    public void email_send()
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("your [email protected]");
        mail.To.Add("[email protected]");
        mail.Subject = "Test Mail - 1";
        mail.Body = "mail with attachment";

        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
        mail.Attachments.Add(attachment);

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

    }
}
}

As you can see, your email_sendmethod is now inside the class declaration.

如您所见,您的email_send方法现在位于类声明中。