58

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

1

3 Answers 3

68

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

var x = new global::test();

2
  • 1
    Thanks David - example worked perfect. Your answer was 10 seconds before @itsme86 - award to you sir. +1 for example... Aug 25, 2014 at 17:51
  • 1
    This solved an ambiguous reference due to a class named the same. One had a namespace and one didn't. It was attempting to call the one with the namespace and getting an exception: error CS0117: 'System.Web.Mvc.HtmlHelperExtensions' does not contain a definition for 'RenderPartialViewToString
    – GarDavis
    Jul 22, 2015 at 15:55
14

Types not defined within a namespace will be in the global namespace.

The global contextual keyword, when it comes before the :: operator, refers to the global namespace, which is the default namespace for any C# program and is otherwise unnamed.

The following example shows how to use the global contextual keyword to specify that the class TestApp is defined in the global namespace:

C# class TestClass : global::TestApp { }
5

In the addition to above answers, it is important to note, what all Type, regardless of its declaration location, has a "fully qualified name", which begins from "global::"

From "O'Relly. C# in a Nutshell":

All type names are converted to fully qualified names at compile time. Intermediate Language (IL) code contains no unqualified or partially qualified names

1
  • It's true that you can program in C# using fully qualified objects and avoiding using statements. The using statements are purely syntactical sugar which compresses the code to make it easier to read. When there is a large degree of confusion around classes with similar names it may make sense to fully qualify types and not have using statements for extra clarity. Jan 28, 2020 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.