Namespace and Using

Introduction

Namespaces are a critically important concept that sadly is ignored by so many Unity developers including Unity its self from time to time. This article will help you understanding what a namespace is and why its absolutely critical that you always define your code within a namespace. The proper use of namespace is important not only for your own good, but also for anyone that will use your code or who will author code that you use.
There is never an occasion that you can forgo using a namespace

Why?

The main need in the context of game development is disambiguation (to make something non-ambiguous).
So for example if you created a class such as:
1
public class Input
2
{
3
//...
4
}
Copied!
You can clearly see why this would be bad if you don't use a namespace, since this will be ambiguous with UnityEngine.Input in any MonoBehaviour
1
using UnityEngine;
2
3
public class MyClass : MonoBehaviour
4
{
5
public void Foo()
6
{
7
Input //This is your custom input not UnityEngine.Input
8
}
9
}
Copied!
Ambiguity such as what is shown here is a common cause of errors. Less so with something such as Input but more so with commonly used names such as Player, Character, CameraController, Item, etc.
Someone makes a class with that name, doesn't put it in a namespace and has now broken every case that did use a proper namespace because the compiler will resolve to the object in the global namespace first.
You may be thinking 'well just name it unique'
That is exactly what a namespace does, but in a way more useful than just really long or cryptic/decorated object names.
namespaces are used by the IDE and compiler for context, driving autocomplete and other features that guide and speed up your development and reduce logical errors. Namespaces have additional features such as aliasing which given even greater flexibility to the developer using them when compared to long or decorated naming.
Countless others have said it better; just google "Why should I use a namespace" maybe add "in C#" or "in Unity" if you want to narrow those results.
C# | Namespaces - GeeksforGeeks
GeeksforGeeks
Use of Namespaces in C#
Stack Overflow
C# Programming/Namespaces - Wikibooks, open books for an open world

Using

Understanding the using directive and statement can save you a lot of time and headache.
using directive - C# Reference
docsmsft

Aliasing

Lets take a look at a common use case for alias use.
1
HeathenEngineering.SteamworksIntegration.API.Input
Copied!
That is the fully qualified name of a static class in the Steam API. Lets assume your not using aliases.
1
using UnityEngine;
2
using HeathenEngineering.SteamworksIntegraiton.API;
Copied!
Can you see the issue we are going to have?
The compiler knows both namespaces contain a class Input so if you try to call Input the compiler will throw an error and ask you to fully qualify the name, which is a really long name in the case of Steam API
😂
1
//For the Steam API one
2
HeathenEngineering.SteamworksIntegration.API.Input;
3
4
//For the UnitEngine one
5
UnityEngine.Input;
Copied!
Now the better way: Aliasing the namespace
1
using Unity = UnityEngine;
2
using API = HeathenEngineering.SteamworksIntegration.API;
Copied!
Now you can qualify the names with a much shorter token
1
//For the Steam API one
2
API.Input;
3
4
//For the UnityEngine one
5
Unity.Input;
Copied!
But wait, there's more
Did you know you can use a static class like a namespace and even alias it?
You should if you read the article I linked above, but here is an example. In the Steam API all the static classes such as Input have at least 1 sub class, this is because the API differs for Server and Client. So to do something you might call:
1
Input.Client.GetControllers;
2
//or
3
Input.Client.GetAction(...);
Copied!
You can see we have to call the subclass Client every time and that is a bit annoying, so we could use the Input.Client as a namespace and alias it
1
using SteamInput = HeathenEngineering.SteamworksIntegration.API.Input.Client;
Copied!
So now we can say
1
SteamInput.GetControllers;
2
//or
3
SteamInput.GetAction(...);
Copied!
Why not just name it SteamInput to begin with?
SteamInput wouldn't be unique in every game, us using a long namespace with nested static classes insures the name is absolutely unique and there is no ambiguity.
You can then decide what you want to call it in short hand for your script, and it can have a different alias in every script if that suits your needs. using this model we give you the flexibility. If we didn't use a namespace or used a flat namespace with decorated names, you would have to take what we give you; which may well collide with other tools your using.