c# call constructor from constructor

63

c# call constructor from constructor -

using System;

namespace call_another_constructor
{
    class sample
    {
        public sample()
        {
            Console.WriteLine("Constructor 1");
        }
        public sample(int x): this()
        {
            Console.WriteLine("Constructor 2, value: {0}",x);
        }
        public sample(int x, int y): this(x)
        {
            Console.WriteLine("Constructor 3, value1: {0} value2: {1}", x, y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            sample s1 = new sample(12, 13);
        }
    }
}

Comments

Submit
0 Comments