- Home /
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?
Answer by MaT227 · Aug 06, 2014 at 09:58 AM
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();
Thank you, I get it now! At first when I wrote the problem here, It wasn't showing me in the console that There is a conflict between this to Random Classes. Now it's shows and I figured out, thank you again.!
Answer by vexe · Aug 06, 2014 at 09:59 AM
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
Thank you, Im using just to get the index from a specific position in the Array, not for something else.
Yes just use a regular array:
Vector3[] vectors;
int[] ints;
etc[] etcs;
Or generics:
List<Vector3> vectors;
List<etc> etcs;
using Random = UnityEngine.Random; //aliases are time savers :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613155945im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# Unity 3D Using functions from other files. 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers