Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The base class should, by design, encapsulate functionality that will be common to all derived classes (since it will be inherited by them). If the field/property that you are initializing through the base class constructor is relevant to the initialization of all derived classes, then yes, passing it to the base class constructor through the <code>base</code> keyword is the correct approach.</p> <p>In your case, since the base class is abstract and its only constructor requires a parameter, then using the <code>base</code> keyword actually becomes mandatory; there is no other way of initializing an instance from the class hierarchy.</p> <p>I am assuming that the example you provided is contrived for the sake of the question. You should <em>not</em> hard-code a class name; you may use the <code>GetType</code> method (or the <code>is</code> keyword) for identifying an instance’s actual type at run-time:</p> <pre><code>MyBase x = new MyChild_One(); Console.WriteLine(x.GetType().Name); // Outputs "MyChild_One" </code></pre> <p>For fields/properties whose values will never change, you could also use an abstract read-only property that is overridden by all derived classes:</p> <pre><code>abstract class MyBase { public abstract String l_strChildName { get; } } class MyChild_One : MyBase { public override String l_strChildName { get { return "MyChild One"; } } } class MyChild_Two : MyBase { public override String l_strChildName { get { return "MyChild Two"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload