找不到类型或命名空间名称"硬件"(是否缺少 using 指令或程序集引用?



错误CS0246在visual c#中找不到类型或命名空间名称"Hardware"(是否缺少using指令或程序集引用?(错误CS0246找不到类型或命名空间名称"Hardware"(是否缺少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");
}
}
}

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

class Hardware : Product
{
public Hardware()
{
Console.WriteLine("Hardware");
}
}

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

要解决此问题,请在Program类中添加一个Using语句,让该类"找到"您的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();
}
}
}

相关内容

最新更新