mSours
mSours

Reputation: 43

C# - How to create constructor for inherited class where parent class only has static constructor

For example, say I wanted to create a class that inherits System.Diagnostics.StopWatch, and for this example pretend that System.Diagnostics.Stopwatch.StartNew() is the only public constructor for that class (I know its not, but I'm trying to inherit a different class where that is the case) :

public class Example : System.Diagnostics.Stopwatch
{
    public Example()
    {
        // ... return System.Diagnostics.Stopwatch.StartNew();
    }
}

I know there are obvious workarounds, but just wondering if this is possible in C#

Upvotes: 2

Views: 146

Answers (1)

InBetween
InBetween

Reputation: 32770

There are basically three scenarios where you can't inherit from a class:

  1. The intended parent class is declared as sealed, which prohibits inheriting from it.
  2. The intended parent class doesn't have an accessible constructor.
  3. The intended parent class is a static class.

If you are in one of these 3 scenarios, you will not be able to inherit from that class, plain and simple, don't look for a usable workaround because there isn't.

Upvotes: 4

Related Questions