Namespace order c#.

Hi guys. Im having some problems here with using namespaces.

I need this 3 namespaces.

using System;  //for Array
using System.Collections; //For Random.Range and IEnumerators
using System.Collections.Generic; // For List

My problem is that If I’m deleting System than I can work with Random.Range and I can’t find Array, if I’m deleting System.Colections, I can work with Arrays but I can’t find Random.Range and Ienumerator.

Is there a way to place this namespaces in a specific order or how should I face this problem?

Who said order matters?

With the System namespace you have access to the System.Random
With System.Collection you have access to the non-generic versions of all collections (including IEnumerator etc) - So Random isn’t located in System.Collections but just System.

And one more thing, don’t use the Array or ArrayList classes, they’re legacy stuff. Left-overs from old C# versions. They’re slow because of boxing and unboxing (which is solved when MS introduced generics)

About the Random class, there’s a System.Random and a UnityEngine.Random - I think you want the latter. But, if you’re adding references to both the namespaces and you type “Random” you’ll get an error saying the call is ambiguous. You could either explicitly mention UnityEngine.Random (or System.Random) or you could just add an alias:

using System;
using etc;
using Random = UnityEngine.Random;

And then just say “Random” and it would resolve to UnityEngine.Random

The order of namespaces is not important. You can find a lot of informations concerning namespaces like this one C# - Namespaces

It seems that your problem concerns the Random class because there is a conflict between System.Random and UnityEngine.Random.

When you face those kind of conflicts, you need to specify the namespace when you call for an object or a function.

System.Random().Whatever();
// or
UnityEngine.Random().Whatever();