3
Reply

Are C# constructors the same as C++ constructors?

Samir Bhogayta

Samir Bhogayta

Jun 25, 2016
238
0

    Not

    Mukesh Kumar
    September 30, 2017
    0

    Well, In C++ as well the constructor of the subclass can call the constructor of the base class and as well the other constructors

    Sripathi Rao
    July 07, 2017
    0

    Very similar, but there are some significant differences. First, C# supports constructor chaining. This means one constructor can call another:class Person { public Person( string name, int age ) { … } public Person( string name ) : this( name, 0 ) {} public Person() : this( “”, 0 ) {} } Another difference is that virtual method calls within a constructor are routed to the most derived implementationError handling is also somewhat different. If an exception occurs during construction of a C# object, the destuctor (finalizer) will still be called. This is unlike C++ where the destructor is not called if construction is not completed.Finally, C# has static constructors. The static constructor for a class runs before the first instance of the class is created.Also note that (like C++) some C# developers prefer the factory method pattern over constructors.

    Samir Bhogayta
    June 25, 2016
    0