-3

错误 CS0246在 Visual c# 中找不到类型或命名空间名称“硬件”(您是否缺少 using 指令或程序集引用?) 当我们在 C# 控制台应用程序中的 Visual Studio 2k19 中运行此程序时出现问题显示:错误 CS0246找不到类型或命名空间名称“硬件”(您是否缺少 using 指令或程序集引用?)

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

namespace OOP1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hardware hf = new Hardware();   // error in this line

            Console.ReadLine();
        }
    }
}

--------------

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

namespace OOP1.com.inventoryMsystem
{
    class Hardware : Product
    {

        public Hardware()
        {
            Console.WriteLine("Hardware");
        }
    }
}

----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OOP1.com.inventoryMsystem
{
    class Product
    {

        public Product()
        {
            Console.WriteLine("Product");
        }
    }
}
4

1 回答 1

1

您正在实例化对 的引用Hardware,这是您创建的类,如下所示:

class Hardware : Product
    {

        public Hardware()
        {
            Console.WriteLine("Hardware");
        }
    }

但是,Program您所指的Hardware班级namespace与您的Hardware班级不同。 ProgramOOP1命名空间中,HardwareOOP1.com.InventoryMsystem命名空间中。因此,您的Program班级并不真正知道您指的是什么。

要解决这个问题,请在您的课程中添加一条Using语句Program,让此类“找到”您的Hardware课程:

using OOP1.com.InventoryMsystem

您完成的Program课程代码应该与此非常相似:

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

using OOP1.com.InventoryMsystem;

    namespace OOP1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hardware hf = new Hardware();   // No error now

                Console.ReadLine();
            }
        }
    }
于 2020-04-26T17:10:44.210 回答