C# Namespaces

Today I’m talking about namespaces when writing C# code.While sometimes not needed in small projects or with solo developers, they are extremely important when things get more complex.

Namespaces allow classes with identical names

Namespaces are a useful feature in C# (and other languages) which allows multiple classes to have the same name, as well as allowing classes to reference classes from other project parts, assets, and systems.

One reason you might want to have classes with duplicate names is to keep classes of similar functions separate and organized. For example, both your player and NPCs might need to move, and so have a “Movement” script, but both would need different code.

While you could easily name one script “PlayerMovement” and the other “NpcMovement” and have that work fine, this solution may not work for more complex projects.

That’s where Namespaces come in.

Namespaces are used in a C# class by adding a “using” directive at the top of the script, like this:

New custom Namespaces can also be created like this:

So in summary, Namespaces are a great way to keep the different systems of your project separate and organized.

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer since 8yo (18 years). Currently looking for games industry employment