How to call other constructor from default constructor in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class MyClass
    {
        public MyClass() : this(2, 3) 
        {
            Console.WriteLine("default constructor");
        }

        public MyClass(int a, int b)
        {
            Console.WriteLine("int a, int b: {0} {1}", a, b);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass myclass1 = new MyClass();
        }
    }
}


/*
run:
    
int a, int b: 2 3
default constructor

*/

 



answered Apr 25, 2017 by avibootz

Related questions

1 answer 149 views
1 answer 125 views
125 views asked Sep 8, 2020 by avibootz
1 answer 186 views
1 answer 161 views
1 answer 236 views
...