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.

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.

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.