- Home /
Checking an array variable in C# vs JavaScript
I'm new to Unity and C# (I must learn C# to work with some people), but the best tutorials guiding from basics to game creation I've found only cover JavaScript, so I've been following along, translating JavaScript to C# where needed.
When it came time to create an IF statement checking on an item in a string array, I ran into a problem. The JavaScript code works like this:
var inventory = Array("food", "gun", "ammo");
if ("food" in inventory)
print("You have found " + inventory[0]);
The closest equivalent I could find (most resources are full of technical terms I don't yet understand) is C#'s "foreach," which looks like this:
string[] inventory = {"food", "gun", "ammo"};
foreach(string myItems in inventory)
{
if(myItems == "food")
print("You have found " + inventory[0]);
}
Is that a suitable way to do it? I heard that foreach is a loop (not sure if the JavaScript use of "in" is too), so I wasn't sure if that meant it just keeps going on and on in the background, taking up resources or something else inefficient. All I wanted to do at the moment was check on a string in an array.
It is a good practice to accept answers or at least upvote them.
Answer by Landern · Nov 08, 2012 at 09:27 PM
string[] inventory = {"food", "gun", "ammo"};
foreach(string item in inventory)
if(item == "food")
print("You have found " + item);
item carries the value of the current item from the inventory collection. This is suitable for display as it is just a string.
Answer by valyard · Nov 08, 2012 at 09:25 PM
The misconception here is because in JavaScript Object and Array don't differ much. The later has length property but you can interchange them freely. In c# Arrays are more like good old arrays in C. But they have lots of extending methods like IndexOf for example. They are basically doing the same as your code does internally but at least your code gets cleaner.
Also you may want to look at List and Dictionary types. Which are more powerful and have better search time than arrays.
arrays perform better then a List does, it's memory foot print is also smaller with the same type because the object graph is smaller.
That said, List/Collection/Hashset/Dictionary are superduper awesome functions
so if you have a set collection that you know the length/count of, use an array, unless you need the extension methods for list and such
Perform better where? It is still O(n) to look for a specific element.
Array access is significantly faster than List access for primitive types (not so much for things like GameObject). Search time is equal; Lists are really just arrays anyway with "extra stuff" that can make them easier to work with for some things.
Just to add to Eric's point - if you inspect a List in the debugger you will see it actually contains an array which is what is used to get the data. Lists allocate arrays of extra size and when they fill up, it allocates another bigger array - these will increase in size based on how many times you add things to them.
If you are vaguely aware of the O notation then:
Arrays are O(1) for indexed access, Dictionaries and Lists approach O(1) performance for accessing an item by index with lists being faster (and dictionaries not necessarily using an integer index). This basically means that it doesn't matter how many things there are in the collection, the access time will be the same.
However finding a thing in a List or an Array by its value is an O(n) operation - dependent on the n number of things in the list (in other words slow and probably not scalable). The time to access something in a SortedList using binary search is O(log n) - (read much much faster) at the penalty of inserting something into the list being slow (so good for many searches and few writes). You will probably never use a SortedList though unless you really need sorted data because of Dictionaries being O(1) for finding an item based on a particular value.
So just to reiterate, Dictionaries are fantastic at finding things by the key value (which of course isn't necessarily an index integer) where they are O(1) - it doesn't get more scalable than that.
Lists are perfect for non-sparse (not many values are empty) integer indexed items that may grow.
Arrays are perfect for non-sparse integer indexed items that do not grow.
Answer by digitalfront · Nov 08, 2012 at 11:09 PM
If you use Lists with System.Linq you can do searches like this myList.Where(item => item == "food");
I'm sure it adds some overhead, but might be worth it.