This () and base () in C #

There seems to be no language syntax for specifying both the this () constructor and base () constructor. Given the following code:

public class Bar : Foo { public Bar() :base(1) //:this(0) { } public Bar(int schmalue) :Foo(1) { //Gobs of initialization code ... Schmalue = schmalue; } public int Schmalue { get; private set; } } public class Foo { public Foo() { Value = 0; } public class Foo(int value) { Value = value; } public int Value { get; private set; } } 

The compiler gives me a message that '{' was expected when uncommenting the call to this: (0). This is annoying because it forces me to decompose my code into a private method when this function was explicitly provided to prevent such a thing.

Am I just doing it wrong? I'm not trying to use a separator, comma, comma ... It seems that it was just control over the development team. I am wondering why this was omitted if I am going to do it wrong or if someone has good suggestions for alternatives.

+6
source share
5 answers

You can accomplish this by calling the base constructor at the end of your chain:

 public Bar(): this(0) {} public Bar(int schmalue): base(1) { // ... initialize } 
+8
source share

No, you can only bind to one constructor - either another constructor in the same class, or a base constructor.

It is not clear what you are trying to do. I usually find it worthwhile to create one "primary" constructor in a derived class: all other constructors in the derived class use "this" to call the primary, which calls "base" to call the corresponding base constructor.

So far, this model is not suitable for each scenario - in particular, when you want to call different base constructors from different derived constructors - this is usually a good model.

+3
source share

Consider what happens if you can name both this and base as constructors in a single constructor. Suppose the base constructor is called first. Then this constructor will be called - which itself will call the base constructor. Thus, the base class will be created twice. This violates the semantics of the constructors, namely that the object is built once.

Thus, calling both base and this forbidden. Let your delegated this constructor call the base with certain parameters, if necessary.

+3
source share

In your design, I don’t see much in common between the Bar and Foo classes. Why Foo flow out of Bar when you redefine everything? Both classes have an integer property with public getter and private setter, and both classes have default constructors and a constructor that allows you to initialize the integer property. So why do these two classes exist anyway?

+1
source share

The second ctor in Bar is simply wrong. Try:

 public Bar(int schmalue) :base(1) //would call Foo(1) { //Gobs of initialization code ... Schmalue = schmalue; } 

The comment in the first ctor seems to mean something like

  • initialize the panel with schmalue = 0

  • calling ctor foo base with value = 1

Right?

To do this, replace the second ctor and just add another (closed) ctor that can handle both values

 public Bar(int schmalue) :this(1, schmalue) //would call Bar(1, schmalue) { } private Bar(int value, int schmalue) :base(value) { //Gobs of initialization code ... Schmalue = schmalue; } 
0
source share

All Articles