When to use a namespace?
This is a noob question.
When I code I usually use class from MonoBehaviour. Now reading others code I see that sometimes they use namespace.
When is it better to use namespace? And how I use them?
Moreover, not sure if it's a related question: what's the difference between MonoBehaviour and namespace?
Thank you, as always.
Answer by Landern · Oct 03, 2014 at 06:26 PM
When I code I usually use class from MonoBehaviour. Now reading others code I see that sometimes they use namespace.
MonoBehaviour is a class in namespace UnityEngine. When you're reading others code, a namespace definition is not a class definition.
Example:
namespace SomeGame.Player
{
public class Hero : MonoBehavior
{
// ...
}
}
When is it better to use namespace? And how I use them?
Use a namespace to ensure your scoping of classes/enum/interface/etc won't conflict with existing ones from other namespaces or the global namespace. Namespaces scope classes, this is important due to many times similar naming conventions are used for class names. This can confuse the compiler and exceptions are thrown. You can explicitly without importing or using the namespace create/invoke in method.
The Unity namespace documentation explains this pretty well.
Moreover, not sure if it's a related question: what's the difference between MonoBehaviour and namespace?
MonoBehaviour is a class in namespace UnityEngine. When you're reading others code, a namespace definition is not a class definition.
MonoBehaviour is a class that other classes derive from, it exists in the UnityEngine namespace and it derives from Behaviour, Behaviour derives from Component and Component derives from UnityEngine.Object.
When a class/type is instantiated, it is constructed in such a way that it begins from the base class up. Depending on how access specifiers are used, you may have access to methods, properties, indexers, fields, etc from all the deriving classes.
Answer by Owen-Reynolds · Oct 03, 2014 at 07:06 PM
Namespaces are in C# for people who already know what they are, and like them. Since you don't, probably best to never use them.
C# is a real programming language, used for lots of things. It has many, many, many extra features, which no one person will ever use all at once. Experienced programmers get in arguments about the correct way to set things up, the best commands, and even features that should never be used.
Namespaces are mostly used by people with traditional computer science backgrounds. Most basic Unity scripts, written by regular game programmers, aren't going to bother with them.
A lot of times, you find a script that you can tweak to do what you want, but you have to figure it out first. If it uses namespaces, it probably also uses a few other tricky things you haven't learned yet, and you aren't going to be able to figure it out enough to use it.
Haha that's a rather depressing answer ;)
To elaborate: I think one of the most common situations where you might want to use a namespace is when you are going to share your code with other people and you want to avoid na$$anonymous$$g collisions with existing scripts in their projects.
I guess you could look at namespaces being a bit like a library of code.
A collection of classes that can be imported to other scripts when needed; with the syntax:
using NameSpace.Whatever; // import a collection of useful classes
I guess it's a good thing namespaces exist, but you probably won't have to worry too much about them unless you plan on creating a c# library for the asset store and you are worried about potential duplicate class names.
Hope that's correct info.
I've computer science background :). But it is old background and I want to refresh it. And now that I've a few answers I think I understand the basics of namespace.
And, btw, maybe I'll not use them, but since I'm working with others code, I need to understand them.
They don't seem that complex now.
So thank you for your answer :).
In practice, Unity people often make a class with all public static functions and variables, which functions pretty much the same as a namespace. Esp. since C# uses the dot for both (in A.B
you can't tell if A is a class, or an instance of a class.)
Then, you often want to use the Inspector to set a few variables, or start a coroutine, which means you're using the $$anonymous$$onobehaivior class with the script on an empty.
I'd like to down vote this, sorry. The fact is that namespace is EXTRE$$anonymous$$ELY important! If everybody don't use it, I can guarantee that nobody can write a game for production. Why? Think about it, nowadays almost EVERY game developer use Assets, you may import lots of scripts with same names like "PlayerController", "Scene$$anonymous$$anager" etc. Namespace is the ONLY way to protect you from infinite rena$$anonymous$$g. It IS very simple by the way.
//In class A
namespace $$anonymous$$yAwesomeNameSpace{
Class A{
//Whatever
}
}
//In other scripts.
using $$anonymous$$yAwesomeNameSpace;
I can't downvote this because I don't have enough reputation here, but I would if I could because it is not a useful answer. "Oh, if you don't know what they are, don't worry about it! They're for people who know more than you," is basically what you're saying here, which defies the point of having an answers forum in the first place. On top of not actually answering the question, it's patronizing.
I'm going to put this comment here to try and help you remember to downvote when you are able.
I didn't even say the worse thing about this Q: it's just a general C# question, and doesn't belong in a Unity area. It's not the askers fault, but the answerers. $$anonymous$$uch, much, much better explanations of every aspect of C# are in sites like stackoverflow. Everyone who doesn't tell you this is trying to keep down the competition by making sure you never learn to code.
It's not patronizing. If a "noob" (OP's words) wants to know when to use a namespace, the best answer is "don't -- it's an advanced thing you don't need now."
If you're going to reject questions like this as a matter of policy, fine. If not, answer it or ignore it. A wishy-washy half-answer isn't going to help anyone. That said, I think this has gone off-topic for long and far enough and will not be responding to this anymore.