[英]missing using directive assembly reference error in c# vscode
When I try to compile a simple application I made in VSCode with 2 .cs files one with a class, and the other with the main method, I get an error: cannot find type or namespace "MyClass"
. 当我尝试使用2个.cs文件和一个main方法编译一个在VSCode中创建的简单应用程序时,出现一个错误:
cannot find type or namespace "MyClass"
。 I do not understand why this happens, as I have put both the main method and class declaration in the same namespace. 我不明白为什么会发生这种情况,因为我已经将main方法和类声明都放在了相同的名称空间中。
MyClass.cs MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test.src
{
public class MyClass
{
public void testFunc() { Console.WriteLine("Hello World!"); }
}
}```
Program.cs Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test
{
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.testFunc();
Console.WriteLine("Hello World!");
}
}
}
I want the console to print Hello World!
我希望控制台打印
Hello World!
, but instead I get this error. ,但出现此错误。
classes_test.src
and 和
classes_test
are not the same Namespace. 不是相同的命名空间。 In order to set your class visible to the main method, you have to use the following using statement.
为了将您的类设置为对main方法可见,您必须使用以下using语句。
using classes_test.src;
This is a quite common mistake, especially for those who are c#-newbie. 这是一个非常普遍的错误,特别是对于那些使用c#-newbie的人。 Including a namespace, DOES NOT include the subnamespaces
包括名称空间,不包含子名称空间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.