错误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
中。Program
在OOP1
命名空间中,Hardware
在OOP1.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();
}
}
}