GameDev and C# : the most used methods for Arrays and Lists
BEWARE OF THIS: I pass Microsoft’s C # documentation because is really useful in my opinion, but we have to watch out because some of that things in Unity work in different way.
Now, this is the last part about arrays and Lists.
The part we need to know is what is the Collections and how use them, ok?
After we understand the logic, we do not need to MEMORIZE EVERYTHING.Is nearly IMPOSSIBLE and USELESS!!
When we have to solve some type of problems that concern Collections, we have documentation. That’s it.
Inside every class of Unity and C#, there are a TONS OF NAMESPACE, has sense try to LEARN EVERY SINGLE PART OF THEM? ABSOLUTELY NO!
One of that, with Arrays, is
This return to us the lenght of our Arrays.
Normally, this is very useful inside a For Loop, because we can set the i = 0, then i < our array .Lenght, i++ and the loop runs for every elements inside of it.
We create a For Loop
This means run the Loop for every single element inside the Array. This is incredibly useful, especially in this loop.
Now I create a List, and try the same thing
The For Loop is IDENTICAL, we only need to change the syntax
The only difference, for this, is :
for Array we use .Length,
for List we use .Count
but the logic not change
Always in Array, we have
With this we can Copy our Array to ANOTHER ARRAY from one SPECIFIC INDEX.
In fact, it ask us two parameters, an Array to Destination, and the int INDEX.
Instead, in the List, we have something more, like
This is where Lists shine :
with this we can literally add elements in our List at RUNTIME.
This is probably the most used thing about List.
Then we have, always for the List
.Clear, like the word said, CLEAR OUR LIST.
Simply we use it and the List becoming Empty.
is another great thing about List
with this we can search inside our List for SPECIFIC ELEMENT.
We can pass this method directly in the If Statement to search for something
then we run the Code.
.Contains is automatically set itself in the type of our List, we have int so ask for int, if we have a string it ask us a string. That’s why I pass the 8, because is an int, and we have a List of int.
Seems stupid, but is good to know how work things.
Even the List have .CopyTo
this method copy the elements inside the List into AN ARRAY.
The last tip here is about
Every of this methods are really useful for search inside our List a specific element at specific index.
That’s it for this part, if you need a specific method go to the documentation and search.
This is the best advice I can give.
Again, it is nearly impossible (and useless) to remember every single method for every single class.
The best way to do this is to understand the basics of
“How does Array work?”
“How does a list work?”
Or more simply, how does Collection work?
After understanding THIS, the rest isn’t easy, but it’s not THAT hard.
Because if you know what a Collection is, you know how to create it and what is the logic behind it, you can use THAT LOGIC to solve the problems that concern it.
And yet, to have a problem and don’t know how solve it is COMPLETELY NORMAL.
You simply take the problem, divide it, and start to think how resolve it piece by piece.
See you in the next article about our 2D Shooter!!