You can't do that. If the constructor is public you can call it from constructors of derived classes. But you can do something close – you can have a private constructor and a public static method that calls it: class MyBase { private MyBase(string someParam) { // some code } public static MyBase Create(string someParam) { return new MyBase(someParam); } protected MyBase() // or some other protected or public constructor { } } class MyDerived : MyBase { public MyDerived() : base("foo") // won't compile, as requested { } }.
You can't do that. If the constructor is public, you can call it from constructors of derived classes. But you can do something close – you can have a private constructor and a public static method that calls it: class MyBase { private MyBase(string someParam) { // some code } public static MyBase Create(string someParam) { return new MyBase(someParam); } protected MyBase() // or some other protected or public constructor { } } class MyDerived : MyBase { public MyDerived() : base("foo") // won't compile, as requested { } }.
If you don't have any constructors accessible to child classes, you might as well just make your class sealed. – StriplingWarrior Sep 30 at 0:32 This is a very good answer and the sort of workaround I was looking for. Thanks!
– Valamas Sep 30 at 0:33 @StriplingWarrior, right, but from the question I assumed there is at least one other constructor usable from derived classes. I have clarified the code in my answer. – svick Sep 30 at 0:37 Ah, that makes more sense.
+1 – StriplingWarrior Sep 30 at 0:45.
All constructors are "sealed" in that they cannot be "overridden. " They can only be called from the constructor in a child class. If you are hoping to prevent child classes from having a constructor with the same signature, that cannot be done.
Based on the additional information you added to the post, it sounds like what you want to do is make your constructor private, as Kyle suggested. This will prevent the child class from calling the constructor, but it won't prevent it from taking the same types of arguments: public class Foo { private Foo(string s){ } // Allowed public Foo() : this("hello") { } } public class Bar : Foo { // Allowed public Bar(string s) : base(){ } // Not allowed public Bar(string s) : base(s){ } }.
I thought as much. Thanks for confirming. – Valamas Sep 30 at 0:16 @Valamas: I added a little more information based on your updated question.
– StriplingWarrior Sep 30 at 0:39.
If you want to prevent the constructor from being called by inherited classes, just mark it private. Constructors aren't inherited by a child class, you have to explicitly call a base constructor if desired. This code will call the base class' no-parameter constuctor when an instance of the child class is instantiated.
Without it, the base class' constructor won't be called when creating a new instance of the child class. Public class A { public A() { } } public class B : A { public B() : base() { } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.