0
[Serializable]
public class DccFormatterException : Exception
{
    public DccFormatterException()
    {}

    public DccFormatterException(string message): base(message)
    {}

    public DccFormatterException(string message, Exception inner): base(message, inner)
    {}

    protected DccFormatterException(SerializationInfo info, StreamingContext context): base(info, context)
    {}
}

I understand declaring the class. But what does overriding the constructors accomplish? They even have the same scopes as the originals.

BWhite
  • 713
  • 1
  • 7
  • 24
  • @kenny I still don't understand what you mean. At the time, I thought it was lack of experience on my part. But all the examples I find follow the pattern in my question and it doesn't seem to work any other way. Can you clarify? – BWhite Aug 05 '16 at 16:26
  • I may have been confused, I'll delete the comment. – kenny Aug 05 '16 at 16:30

1 Answers1

4

Constructors are not "virtual": only a constructor declared for that type can be used to instantiate it.

class A {
    public A () {}
    public A (int x) {}
}
class B : A {
    public B(): base() {} // See <1>
}

var b1 = new B();  // O.K.

var a = new A(42); // O.K.

// Compiler error, as there is no matching constructor in B:
// "'B' does not contain a constructor that takes 1 arguments"
var b2 = new B(42);

Any constructors that are to be exposed via the derived type must be "proxied"; this is not overriding and there is no polyphormism in play.


1 C# does create a default constructor; the declaration for B could have been empty with the same results, but I chose to be explicit - and being explicit does matter if there is any other constructor present.

user2864740
  • 60,010
  • 15
  • 145
  • 220