C# Arrays


Arrays are similar to variables, but can hold more than one value.
DataType[ ] ArrayName = { Comma Separated Values } // Array of any size
DataType[] ArrayName = new DataType[3] {Command Separated Values } //Expects 3 values

class Program  { // Foreach with Arrays
    static void Main(string[] args) {
        int myInt = 0;
        string[] friendNames = { "Sam", "Jack", "John" };
        WriteLine($"{friendNames.Length} of my friends:");
        foreach (string friendN in friendNames)
        {  // foreach gives READ-ONLY access
            WriteLine(friendN);
            ++myInt;  // just a counter
        }
        // or you can use a for loop:
        for (int i = 0; i < friendNames.Length; i++) {
            WriteLine(friendNames[i]); }
        ReadKey();
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *