如何在 C# 中创建用户定义的命名空间
How to make a user defined namespace in c#
我在Visual Studio中有以下两个文件:
//file1.cs
using System;
namespace Pointns{
public class Point{
public float x;
public float y;
public Point(){
x = 0;
y = 0;
}
}
}
//file2.cs
using System;
using Pointns;
namespace test1{
class test2{
static void Main(){
Point p1 = new Point();
Console.WriteLine("Hello World");
Console.WriteLine(p1.x);
Console.ReadLine();
}
}
}
当我尝试编译时它说找不到 Pointns
。我做错了什么?
您应该考虑以下事项:
- 这两个文件是在同一个项目中吗?如果没有,您将需要添加对目标项目的引用(包含 file1.cs 的项目)。
- 这两个文件 "included" 都在您的项目中吗?如果您没有启用 "show all files" 选项,并且在解决方案资源管理器中看不到您的项目下列出的文件,那么您需要将该文件添加到您的项目中(即使它在同一个文件夹中。要做为此,启用 "show all files" 选项(通过解决方案资源管理器顶部的按钮),然后右键单击项目中显示的文件和 select "include in project";或者,您可以右键单击项目并选择 "add > existing item".
我在Visual Studio中有以下两个文件:
//file1.cs
using System;
namespace Pointns{
public class Point{
public float x;
public float y;
public Point(){
x = 0;
y = 0;
}
}
}
//file2.cs
using System;
using Pointns;
namespace test1{
class test2{
static void Main(){
Point p1 = new Point();
Console.WriteLine("Hello World");
Console.WriteLine(p1.x);
Console.ReadLine();
}
}
}
当我尝试编译时它说找不到 Pointns
。我做错了什么?
您应该考虑以下事项:
- 这两个文件是在同一个项目中吗?如果没有,您将需要添加对目标项目的引用(包含 file1.cs 的项目)。
- 这两个文件 "included" 都在您的项目中吗?如果您没有启用 "show all files" 选项,并且在解决方案资源管理器中看不到您的项目下列出的文件,那么您需要将该文件添加到您的项目中(即使它在同一个文件夹中。要做为此,启用 "show all files" 选项(通过解决方案资源管理器顶部的按钮),然后右键单击项目中显示的文件和 select "include in project";或者,您可以右键单击项目并选择 "add > existing item".