Samir Bhogayta
Are C# constructors the same as C++ constructors?
By Samir Bhogayta in C# on Jun 25 2016
  • Mukesh Kumar
    Sep, 2017 30

    Not

    • 0
  • Sripathi Rao
    Jul, 2017 7

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

    • 0
  • Samir Bhogayta
    Jun, 2016 25

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS