gpt4 book ai didi

c# - 如何使用自定义命名空间?

转载 作者:行者123 更新时间:2023-12-04 17:01:52 33 4
gpt4 key购买 nike

在 Mono 基础中,有一个例子:

using System;
using System.Windows.Forms;

public class HelloWorld : Form
{
static public void Main ()
{
Application.Run (new HelloWorld ());
}

public HelloWorld ()
{
Text = "Hello Mono World";
}
}

我想知道如何在 C# 代码中添加自定义命名空间?例如,添加一个新的命名空间

using myComponent;



为了附加一个新的字符串,

myString.FirstString



编译时会报错:
using System;
using System.Windows.Forms;
using myComponents;

public class HelloWorld : Form
{
static public void Main ()
{
Application.Run (new HelloWorld ());
}

public HelloWorld ()
{
Text = "Hello Mono World " + myString.FirstString;
}
}

helloworld.cs(3,7): error CS0246: The type or namespace name 'myComponent' could not be found (are you missing a using directive or an assembly reference?)



下面是自定义的“myComponent”,如何将 Main() 和“myComponent”集成在一起?
namespace myComponent
{
public static class myString
{
public static string FirstString
{
get
{
return "connection";
}
}
} //Class end
}//Namespace end

我该怎么办?即使我在两个文件中使用相同的命名空间(myComponet),我仍然收到错误消息。

谢谢 !!

提供更多信息
1.两个cs文件在同一个目录下

2.执行进度如下

C:\Users\xxxxx\CSharpWorkSpace\Pop20b>csc helloworld.cs -r:System.Windows.Form
dll
Microsoft (R) Visual C# 编译器版本 2.6.0.62309 (d3f6b8e7)
版权所有 (C) 微软公司。版权所有。

警告 CS1668:指定了无效的搜索路径“C:\Program Files (x86)\sql11\LIB”
在“LIB 环境变量”中——“目录不存在”
helloworld.cs(3,7):错误 CS0246:类型或命名空间名称“myComponent”可能
找不到(您是否缺少 using 指令或程序集引用?)

C:\Users\xxxxx\CSharpWorkSpace\Pop20b>mono --version
Mono JIT 编译器版本 5.10.0(Visual Studio 内置 Mono)
版权所有 (C) 2002-2014 Novell, Inc、Xamarin Inc 和贡献者。 www.mono-proj
ect.com

最佳答案

如果已经定义了命名空间,您是否只能使用命名空间。您可以通过在该命名空间中创建类、结构、枚举或其他对象,或包含具有该命名空间的程序集来实现。

如果你把它放在你的代码中,你的使用将起作用:

namespace myComponents
{
public class SomeComponent
{
}
}

关于c# - 如何使用自定义命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316401/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com