Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

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);
        }
    }
}
Source by www.delftstack.com #
 
PREVIOUS NEXT
Tagged: #call #constructor #constructor
ADD COMMENT
Topic
Name
4+3 =