C#: Converting foreach program into OOP style

This entry is part 38 of 71 in the series C#

When it comes to learning OOP style of programming, it is good to convert even a simple program into object-oriented style.

From our previous example, we convert it into using displayArray method to output the array string.

using System;

namespace ArrayDisplay
{
    class Program
    {
        private string[] str = { "The", "End", "Of", "The", "Innocence" };

        private void displayArray()
        {
            foreach (string iStr in str)
            {
                Console.Write("{0} ", iStr);
            }
        }

        static void Main()
        {
            Program display = new Program();
            display.displayArray();
            Console.ReadLine();
        }
    }
}

 

Series Navigation<< C#: Using foreach to print out array elements
C#: Passing parameters into a constructor >>
Posted in

Leave a comment

Leave a Reply