命名空间规范

时间:2012-07-12 09:55:35

标签: c# .net visual-studio

我想在我的控制台程序中使用最简单的消息框(选择空的C#项目类型)。

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;

namespace roughDraft
{
    class Program
    {
        public static void Main()
        {
            MessageBox.Show("Hello :)");

            Console.ReadKey();           
        }
    }
}
  

名称空间中不存在类型或命名空间名称“Forms”   'System.Windows'(你错过了一个程序集引用吗?)

正如您在本案例中所看到的,程序不起作用,尽管我使用的the official documentation指定的名称空间完全相同。

为什么使用较少指定的命名空间(即System.Windows)时没问题?如何决定是否“削减”?

3 个答案:

答案 0 :(得分:4)

命名空间不是“就在那里”。您需要包含对.NET DLL的引用才能访问命名空间。例如,System.Windows.Forms命名空间在System.Windows.Forms.dll dll中定义。创建控制台应用程序时,不会引用此DLL。所以要使用它,你需要引用它。

如果您已创建WPF应用程序,则默认情况下不能使用System.Windows.Forms命名空间。您需要添加对System.Windows.FormsSystem.Drawing的引用,或者使用System.Windows.MessageBox

答案 1 :(得分:1)

编译器告诉你问题是什么:你缺少对System.Windows.Forms.dll的引用。一旦add the reference,就没有问题。

答案 2 :(得分:1)

您需要添加引用System.Windows.Forms.dll才能使用它。要添加引用,请按照以下步骤操作:

  1. 在Solution Explorer中,右键单击项目节点,然后单击Add Reference。

  2. 在“添加引用”对话框中,选择.NET选项卡,然后选择System.Windows.Forms并单击“确定”。

相关问题