Namespace and Using
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.
One major issue with learning to program through Unity, is that Unity is absolutely horrible with how it handles its coding style in particular around common conventions like namespace.
Strangely Unity considers the use of namespaces as an "intermediate" concept, and only references it as import for code organization. However, this couldn't be further from the truth. Properly formed code is more than just about organizing your class objects, it helps reduce the risk of ambiguity which can result in bugs that don't through errors and can be incredibly difficult to track down even for a skilled engineer.
This topic bares more attention than we will give it here, so just accept that ALWAYS using a namespace in ALL of your code is an absolute must.
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:
public class Input
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
public class MyClass : MonoBehaviour
public void Foo()
Input //This is your custom input not UnityEngine.Input
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.
Understanding the using directive and statement can save you a lot of time and headache.
Lets take a look at a common use case for alias use.
That is the fully qualified name of a static class in the Steam API. Lets assume your not using aliases.
Can you see the issue we are going to have?
The compiler knows both namespaces contain a class
Inputso 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
//For the Steam API one
//For the UnitEngine one
Now the better way: Aliasing the namespace
using Unity = UnityEngine;
using API = HeathenEngineering.SteamworksIntegration.API;
Now you can qualify the names with a much shorter token
//For the Steam API one
//For the UnityEngine one
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:
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
using SteamInput = HeathenEngineering.SteamworksIntegration.API.Input.Client;
So now we can say