[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Create a class outside of any namespace statement in C#

[Create a class outside of any namespace statement in C#]

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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.