What namespace will a class have if no namespace is defined(如果没有定义命名空间,一个类会有什么命名空间)
问题描述
在 C# 中,如果我创建一个没有命名空间的类,我将在尝试实例化该类时使用什么命名空间?
In C#, if I create a class with no namespace, what namespace will I use when trying to instantiate the class?
例如,假设 main 是...
For example, assume main is...
namespace NamespaceTests
{
class Program
{
static void Main(string[] args)
{
}
}
}
...并假设我的无命名空间类是...
... and assume my namespace-less class is ...
public class test
{
public string SayHello()
{
return "Hello World!";
}
}
...并假设我有另一个同名的类,但具有默认命名空间...
... and assume I have another class by the same name, but having the default namespace...
namespace NamespaceTests
{
public class test
{
public string SayHello()
{
return "Hello Moon...";
}
}
}
...我将如何修改 main 以包含无命名空间类的实例并调用SayHello"来检索消息Hello World!"?具体来说,我将如何完全限定类test"的无命名空间实例,特别是考虑到我可能有另一个类也称为test"但有一个命名空间,所以我需要区分......
... how would I modify main to include an instance of the namespace-less class and call 'SayHello' to retrieve the message "Hello World!"? Specifically, how would I fully qualify the namespace-less instance of class 'test', especially considering I may have another class also called 'test' but having a namespace, so I need to distinguish...
推荐答案
在 全局命名空间,可以这样引用:
var x = new global::test();
这篇关于如果没有定义命名空间,一个类会有什么命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果没有定义命名空间,一个类会有什么命名空间


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01