C#: Passing parameters into a constructor

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

When it comes to constructing a constructor, take note that its name is always similar to the class name.

In our example below, the class name is Program and so the constructor name is also Program.

using System;

namespace ArrayDisplay
{
    class Program
    {
        private string[] str;

        private Program(string[] str1)
        {
            str = str1;
        }

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

        static void Main()
        {
            string[] str1 = { "The", "End", "Of", "The", "Innocence" };

            Program display = new Program(str1);
            display.displayArray();
            Console.ReadLine();
        }
    }
}

 

Series Navigation<< C#: Converting foreach program into OOP style
C#: If Else >>
Posted in