Accessing all items of an Array in C#

By | April 18, 2011


In order to access all the items of an array in C#, use the following lines of code. We are using foreach loop to access all the elements of an array in C#.

using System;

class MainClass
    public static void Main()
        string myString = "Apple Ball Cat";
        char[] separator = {' '};
        string[] spellArr= myString .Split(separator);

        foreach (object chk in spellArr)
           Console.WriteLine("Spell: {0}", chk );

It will print out,
Spell: Apple
Spell: Ball
Spell: Cat


Leave a Reply

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