Create a class outside of any namespace statement in C#

[namespace]

When you create a class in C#, Visual Studio automatically puts it inside a namespace statement. If your project’s name is howto_remove_namespace, then by default that namespace is howto_remove_namespace.

But did you know that you can create classes outside of any namespace? The following code shows the file Person.cs used by this example.

namespace howto_remove_namespace
{
    public class Person1
    {
        public string FirstName, LastName;
        public override string ToString()
        {
            return FirstName + " " + LastName;
        }
    }
}

public class Person1
{
    public string FirstName, LastName;
    public override string ToString()
    {
        return "[" + FirstName + " " + LastName + "]";
    }
}

public class Person2
{
    public string FirstName, LastName;
    public override string ToString()
    {
        return "[" + FirstName + " " + LastName + "]";
    }
}

The file begins with a typical namespace statement. Inside the namespace, the file defines the Person1 class with FirstName and LastName properties, and a ToString method.

After the namespace’s closing bracket, the file defines another Person1 class. This version’s ToString method puts brackets around the name so you can tell it’s the second version of the class.

The file finishes by defining a Person2 class, again outside of the namespace block.

If you use the View menu’s Object Browser command, you can use the Object Browser to search for the classes. If you search for Person1, the Object Browser shows only one version of the Person1 class and it says it’s in the howto_remove_namespace namespace. (It’s weird that the Object Browser doesn’t show both versions of the class somewhere.)

Here’s where it gets a little weird. If you comment out either of the versions of the class, the Object Browser still finds the other version and still says it’s in the same namespace. The class that is not inside the namespace block is still in the namespace because that is the default namespace for the project.

Both versions of the class seem to be in the same namespace, which you would think would be a name conflict, but C# seems to be okay with that. If you run the program with both classes defined, C# uses the one that’s inside the namespace block. If you remove that version, then C# uses the other version.

The main program uses the following code to demonstrate the two versions of the Person1 class and the Person2 class.

// Make some people.
private void Form1_Load(object sender, EventArgs e)
{
    Person1 rod = new Person1()
        { FirstName = "Rod", LastName = "Stephens" };
    txtPerson1.Text = rod.ToString();

    Person2 zaphod = new Person2()
        { FirstName = "Zaphod", LastName = "Beeblebrox" };
    txtPerson2.Text = zaphod.ToString();
}

If you look closely at the picture at the top of the post, you can see that the program uses the first definition of Person1. It also uses Person2, which is not contained in the namespace block.

So what’s the point? If you do a lot of copying of code from one project to another, as I sometimes do with these examples, you can remove the namespace statement from a file to make copying the code easier. If you make a Person class and remove the namespace statement from its file, then you can copy and paste the whole file into another project and not need to worry about messing up the namespace statement. Without that statement, the class gets the new project’s default namespace and everything works just fine.

This still leaves a few unanswered questions. When you have both versions defined, where is the one outside of the namespace block? Is it replaced by the one inside the namespace block and thus inaccessible? Is there a way to explicitly access the hidden version? If you know the answer, please post a comment.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in classes, syntax and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Create a class outside of any namespace statement in C#

  1. I think you’re forgetting that CSC will automatically put a class in a namespace regardless of whether you wrap it in a namespace or not. Yes, they appear to be the same in the IDE but that’s not how it actually works inside the IDL. If you look in the IDL, you will see the actual outer namespace along with your ‘inner’ namespace that you see.

  2. Rod Stephens says:

    It’s not that I think the code isn’t in *any* namespace, just that it’s easier to copy and paste because it doesn’t include the namespace statement.

    And if you’re going to reuse significant amounts of code, it would be better to put it in a library and share that anyway.

Leave a Reply

Your email address will not be published. Required fields are marked *