c#编码错误,显示预期的';}';

c#编码错误,显示预期的';}';,c#,C#,我在C#编程中面临一个非常基本的问题,即一个放错位置或丢失的“}”。我检查了所有内容,但仍然显示相同的错误。我正在附加代码。请帮助我更正它。因为它对我非常重要。谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using iTextSharp.text; namespace pdf { class Progr

我在C#编程中面临一个非常基本的问题,即一个放错位置或丢失的“}”。我检查了所有内容,但仍然显示相同的错误。我正在附加代码。请帮助我更正它。因为它对我非常重要。谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;

namespace pdf
{
class Program
{
    static void Main(string[] args)
    {//THIS IS THE PLACE WHERE THE ERROR IS SHOWING 
    private Font _largeFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD,       BaseColor.BLACK); 
    private Font _standardFont = new Font(Font.FontFamily.HELVETICA, 14, Font.NORMAL, BaseColor.BLACK); 
    private Font _smallFont = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.BLACK);
    public void Build() 
    { 
        iTextSharp.text.Document doc = null;
        try 
        { 
            // Initialize the PDF document 
            doc = new Document(); 
             iTextSharp.text.pdf.PdfWriter writer =   iTextSharp.text.pdf.PdfWriter.GetInstance(doc, 
             new System.IO.FileStream(System.IO.Directory.GetCurrentDirectory() + "\\ScienceReport.pdf", 
                System.IO.FileMode.Create));
   // Set margins and page size for the document 
   doc.SetMargins(50, 50, 50, 50); 
   // There are a huge number of possible page sizes, including such sizes as 
   // EXECUTIVE, LEGAL, LETTER_LANDSCAPE, and NOTE 
   doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, 
       iTextSharp.text.PageSize.LETTER.Height));
   // Add metadata to the document.  This information is visible when viewing the 
   // document properities within Adobe Reader. 
   doc.AddTitle("My Science Report"); 
   doc.AddCreator("M. Lichtenberg"); 
   doc.AddKeywords("paper airplanes");

   iTextSharp.text.pdf.PdfPageLabels pdfPageLabels = new    iTextSharp.text.pdf.PdfPageLabels(); 
   pdfPageLabels.AddPageLabel(1, iTextSharp.text.pdf.PdfPageLabels.EMPTY, "Basic     Formatting"); 
   pdfPageLabels.AddPageLabel(2, iTextSharp.text.pdf.PdfPageLabels.EMPTY, "Internal    Links"); 
   pdfPageLabels.AddPageLabel(3, iTextSharp.text.pdf.PdfPageLabels.EMPTY, "Bullet List"); 
   pdfPageLabels.AddPageLabel(4, iTextSharp.text.pdf.PdfPageLabels.EMPTY, "External Links"); 
   pdfPageLabels.AddPageLabel(5, iTextSharp.text.pdf.PdfPageLabels.EMPTY, "Image"); 
   writer.PageLabels = pdfPageLabels; 
   } 
       catch (iTextSharp.text.DocumentException dex) 
       { 
       // Handle iTextSharp errors 
       } 
       finally 
       { 
         // Clean up 
           doc.Close(); 
           doc = null; 
       } 

     }
   }
 }
}

不能在内部定义字段、属性或方法
Main()
-method
可能尝试重新排列您的代码:

static void Main(string[] args)
{
    Program p = new Program();
    p.Build();
}

private Font...
public void Build() 
{
    //...
}

字段应在Main方法之外定义,如下所示:

namespace pdf
{
    class Program
    {
        private Font _largeFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD,       BaseColor.BLACK); 
        private Font _standardFont = new Font(Font.FontFamily.HELVETICA, 14, Font.NORMAL, BaseColor.BLACK); 
        private Font _smallFont = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.BLACK);

        static void Main(string[] args)
        {
            ......
        }
        ......
    }
}

Visual Studio正在将您指向确切的位置

请用}替换
//这是错误显示的地方


至少这可以修复您指出的错误。

这样的缩进如何判断所有内容都是正确的?请以可读的方式格式化代码。这可能会让你的生活更轻松。诊断这些问题的一个小贴士是保存文件的副本,然后慢慢删除方法和代码,直到错误消失(在副本中)。很抱歉以如此业余的方式发布了一个问题。因为它被视为与代码长度有关的“离题”,你可以在我的问题得到回答后关闭它。非常感谢你,你解决了我的问题。如果我的问题看起来有点愚蠢,我真的很抱歉,因为我第一次在这里发布了一个问题,它可能没有被很好地框定。