C# Arrays Part 2


C# arrays part 2.

static void Main(string[] args)  {       // Arrays Part 2
    // declare and initialize an array of strings with three literal values
    string[] friendNames = { "Todd Anthony", "Kevin Holton", "Shane Laigle" };
    int i;
    WriteLine($"Here are {friendNames.Length} of my friends:");
    for (i = 0; i < friendNames.Length; i++)  {
        WriteLine(friendNames[i]);  }
    WriteLine("\nHere they are again:");
    foreach (string friendName in friendNames)  {
        // foreach is READ-ONLY access to each name
        Console.WriteLine(friendName);  }
    // the new keyword explicitly initializes the array; default value is zero.
    int[] myIntArray = new int[5];  
    foreach (int myInt in myIntArray) {
        Console.WriteLine($"five default integers: {myInt}"); }
    // You can use the new keyword to explicitly initialize the array, and
    // a constant value to define the size.
    string[] myStringArray = new string[5]; 
    foreach (string myString in myStringArray)  {
        Console.WriteLine($"five default strings: {myString}");  // myString is null }
    if (myStringArray[0] == null)  {
        Console.WriteLine($"myStringArray[0] is NULL");  }
    myStringArray[4] = "I am the last string in the array. Array indexing starts at zero.";
    Console.WriteLine(myStringArray[4]);
    ReadKey();
}

Leave a comment

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