Hey guys, I had some questions about nesting various data structures and types inside C#. I’ve been digging around the MSDN reference for a while (as well as many other locations on this here internet) and haven’t been able to sufficiently answer my questions.
Here’s a quick question/example. Are the C# “generics” capable of holding built in arrays or are those a different type? For example can I do something like.
I guess what I am asking here is are Vector3 and Vector3[ ] of the same type? Can I mix those in generics? In this case should I just be using an Array or Hashtable and casting the data as I pull it out?
Edit: I misunderstood the question, hpjohn’s first response is what you’re looking for. You can also nest lists within lists if you want (List<List> vectorList).
I would avoid using Array, ArrayList, and Hashtable when at all possible - even if it means a little more coding. That said, you can do both of these:
Why is that? Are you just recommending that I stay away from the non-generic data structures in C# or is there some reason specific to unity (performance or otherwise) why it’s wise to stay away from those. I was under the impression that Arrays where the “list” dataType of preference due to their memory use and blazing fast speed. Am I incorrect?
I remember a time when the generics wouldn’t even function on phones and everyone was going largely out of their way to use basically ONLY the built in array type. I was not aware there had been such a swing.
Ah, no - two different things. Built-in arrays are fine - fast and small, but inflexible, like I said. The thing that’s obsolete is Unity’s own Array class.
Ahh yes, sorry - I should have clarified. Array() is obsolete (it was around before UnityScript got access to Generics to help provide a List<> like structure, but now UnityScript can just use List) - but builtin arrays like Vector3[ ] are still very, very useful and very, very fast.
I stay away from ArrayList and Hashtable as much as possible because type safety is awesome (faster, easier to read and work with, no guessing what the data is on the receiving end, countless other reasons) - that said, sometimes Hashtable in particular is unavoidable (WWW springs to mind) - but in almost all instances Dictionary<,> is better. For the most part, ArrayList and Hashtable are just relics of the days before generics were around to solve the issues in a type safe manner. The generics also make extension methods easy to use and incredibly powerful.
The second one is giving you a built-in array of generic List<>s. When declaring stuff, the [ ] operator specifies that the thing on its immediate left is an array.
Anyway, as the other guys are saying, as a general rule stick to either the built-in array or one of the modern generic collections. Also, when deciding what collection to use, learn a) its interface (how you can add/remove/sort/look things up) and b) how it stores things and finds them, and decide what to use based on how those best suit what you’re trying to do. Getting your data structures right will go a long way towards making the code easier to write.
Further, if you are nesting, and the purpose is create entries of equal length, but you know ahead of time exactly how many (eg a map) consider using
type[,] some2DArray = new type[size1,size2];
multi-dimenional (builtin) arrays, they are neater to manager, and you index them
some2DArray[x,y] (or y,x depending on you)