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