如果未定义名称空间,则类将具有什么名称空间 [英] What namespace will a class have if no namespace is defined

查看:62
本文介绍了如果未定义名称空间,则类将具有什么名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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...

推荐答案

它位于全局名称空间,可以这样引用:

It's in the global namespace and can be referenced like this:

var x = new global::test();

这篇关于如果未定义名称空间,则类将具有什么名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆