How to call default contstructor from another one


One of my team mate asked me this question today. I was not aware of this before that.
Here is a code for the same,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class A
    {
        public A()
        {
            Console.WriteLine("Default");
        }

        public A(string msg) : this()
        {
            Console.WriteLine("this is param : " + msg);
        }
    }
    
Now when you create instance of class A as,

1
A objA = new A("Hello");

It will print statements as;
Default
this is param : Hello

No comments:

Post a Comment