C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ArraysAn array is used to store a sequential collection of values of the same type. In short, an array is used to store lists of values in a single variable. Suppose if you want to store a number of player names, rather than storing them individually as string p1, string p2, string p3, etc. we can store them in an array. Arrays are a way of storing a collection of data of the same type together. Declaring an arrayTo declare an array in C#, you must first say what type of data will be stored in the array. After the type, specify an open square bracket and then immediately a closed square bracket, []. This will make the variable an actual array. We also need to specify the size of the array. It simply means how many places are there in our variable to be accessed. Syntax: accessModifier datatype[] arrayname = new datatype[arraySize]; Example: public string[] name = new string[4]; To allocate empty values to all places in the array, simply write the "new" keyword followed by the type, an open square bracket, a number describing the size of the array, and then a closed square bracket. ExampleLet's see one simple example using an array: using System.Collections; using System.Collections.Generic; using UnityEngine; public class ArrayExample : MonoBehaviour { public int[] playerNumber= new int[5]; void Start() { for (int i = 1; i < playerNumber.Length; i++) { playerNumber[i] = i; Debug.Log("Player Number: " +i.ToString()); } } } Output:
Next TopicUnderstanding Collisions
|