Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c# - What namespace will a class have if no namespace is defined

In C#, if I create a class with no namespace, what namespace will I use when trying to instantiate the class?

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...";
        }
    }
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

var x = new global::test();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...