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










2














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#










share|improve this question





















  • You can't do that.
    – SLaks
    Nov 11 at 17:48






  • 2




    StartNew() isnt a constructor
    – Adrian
    Nov 11 at 17:48










  • The best you can do is public static new Example StartNew() var e = new Example(); e.Start(); return e;
    – Lasse Vågsæther Karlsen
    Nov 11 at 17:54










  • Often classes are designed this way for the specific purpose of preventing inheritance. If there are no accessible constructors then you can't inherit from the class.
    – Mike Zboray
    Nov 11 at 19:18










  • Please show your real case so we can really help you.
    – Aldert
    Nov 11 at 19:23















2














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#










share|improve this question





















  • You can't do that.
    – SLaks
    Nov 11 at 17:48






  • 2




    StartNew() isnt a constructor
    – Adrian
    Nov 11 at 17:48










  • The best you can do is public static new Example StartNew() var e = new Example(); e.Start(); return e;
    – Lasse Vågsæther Karlsen
    Nov 11 at 17:54










  • Often classes are designed this way for the specific purpose of preventing inheritance. If there are no accessible constructors then you can't inherit from the class.
    – Mike Zboray
    Nov 11 at 19:18










  • Please show your real case so we can really help you.
    – Aldert
    Nov 11 at 19:23













2












2








2


1





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#










share|improve this question













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#







c# inheritance constructor static






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 17:47









mSours

213




213











  • You can't do that.
    – SLaks
    Nov 11 at 17:48






  • 2




    StartNew() isnt a constructor
    – Adrian
    Nov 11 at 17:48










  • The best you can do is public static new Example StartNew() var e = new Example(); e.Start(); return e;
    – Lasse Vågsæther Karlsen
    Nov 11 at 17:54










  • Often classes are designed this way for the specific purpose of preventing inheritance. If there are no accessible constructors then you can't inherit from the class.
    – Mike Zboray
    Nov 11 at 19:18










  • Please show your real case so we can really help you.
    – Aldert
    Nov 11 at 19:23
















  • You can't do that.
    – SLaks
    Nov 11 at 17:48






  • 2




    StartNew() isnt a constructor
    – Adrian
    Nov 11 at 17:48










  • The best you can do is public static new Example StartNew() var e = new Example(); e.Start(); return e;
    – Lasse Vågsæther Karlsen
    Nov 11 at 17:54










  • Often classes are designed this way for the specific purpose of preventing inheritance. If there are no accessible constructors then you can't inherit from the class.
    – Mike Zboray
    Nov 11 at 19:18










  • Please show your real case so we can really help you.
    – Aldert
    Nov 11 at 19:23















You can't do that.
– SLaks
Nov 11 at 17:48




You can't do that.
– SLaks
Nov 11 at 17:48




2




2




StartNew() isnt a constructor
– Adrian
Nov 11 at 17:48




StartNew() isnt a constructor
– Adrian
Nov 11 at 17:48












The best you can do is public static new Example StartNew() var e = new Example(); e.Start(); return e;
– Lasse Vågsæther Karlsen
Nov 11 at 17:54




The best you can do is public static new Example StartNew() var e = new Example(); e.Start(); return e;
– Lasse Vågsæther Karlsen
Nov 11 at 17:54












Often classes are designed this way for the specific purpose of preventing inheritance. If there are no accessible constructors then you can't inherit from the class.
– Mike Zboray
Nov 11 at 19:18




Often classes are designed this way for the specific purpose of preventing inheritance. If there are no accessible constructors then you can't inherit from the class.
– Mike Zboray
Nov 11 at 19:18












Please show your real case so we can really help you.
– Aldert
Nov 11 at 19:23




Please show your real case so we can really help you.
– Aldert
Nov 11 at 19:23












1 Answer
1






active

oldest

votes


















4














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.






share|improve this answer






















  • Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
    – PetSerAl
    Nov 12 at 1:00











  • @PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
    – InBetween
    Nov 12 at 5:16











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251485%2fc-sharp-how-to-create-constructor-for-inherited-class-where-parent-class-only%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














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.






share|improve this answer






















  • Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
    – PetSerAl
    Nov 12 at 1:00











  • @PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
    – InBetween
    Nov 12 at 5:16
















4














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.






share|improve this answer






















  • Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
    – PetSerAl
    Nov 12 at 1:00











  • @PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
    – InBetween
    Nov 12 at 5:16














4












4








4






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 20:17

























answered Nov 11 at 19:27









InBetween

25k33967




25k33967











  • Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
    – PetSerAl
    Nov 12 at 1:00











  • @PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
    – InBetween
    Nov 12 at 5:16

















  • Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
    – PetSerAl
    Nov 12 at 1:00











  • @PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
    – InBetween
    Nov 12 at 5:16
















Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
– PetSerAl
Nov 12 at 1:00





Case 2 is not technically correct. In C# 5 you can declare class like this: class B private B() class D: B D(): this(1) D(int i): this() which does not reference base class constructor, thus its accessibility does not matter.
– PetSerAl
Nov 12 at 1:00













@PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
– InBetween
Nov 12 at 5:16





@PetSerAl If that compiles in c#5 (can’t test it now) then it’s a compiler bug, not a language feature. It sure doesn’t compile in the current version, if it did you’d get a StackOverflowException when run.
– InBetween
Nov 12 at 5:16


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251485%2fc-sharp-how-to-create-constructor-for-inherited-class-where-parent-class-only%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo