1 good reason why chain constructors are called first?

Can someone give me 1 good reason why in C # the called constructor is always called before any of the constructor body?

.NET allows you to call a chain constructor anywhere in the constructor, so why does C # force you to do this before your constructor body is executed?

I once wrote to Anders X and asked him about it, and he was kind enough to spend time answering, no matter how busy he was. Unfortunately, he managed to answer a question that I really did not ask (about the named constructors.)

So, just out of curiosity, I thought I would ask here, because personally I don’t think there is one good reason for this restriction, so I hope they will re-educate me :-)

Just to clarify. The rule of CLR.NET is that 1 constructor must be called, only 1 constructor, and only once. So in CLR they are valid

public class Meh
{
  public Meh()
  {
    Console.WriteLine("Meh()");
    this("Hello");
  }

  public Meh(string message)
  {
    Console.WriteLine("Meh {0}", message);
    base();
  }
}

But not in C #

+3
source share
6 answers

If there was a true reason why this ability should not be provided, it will not be supported by the CLR or other .NET languages. I can only conclude that the answer is one of “Because it has always been like this,” and the restriction was probably just copied from C ++ or something like that.

0
source

, , . .

, .

+6

/* , delphi BaseClass   {       public BaseClass ( sSql)       {           ExecSQL (sSql);           //       }   }

public class DerivedClass : BaseClass
{
    public DerivedClass(int i, DateTime dt)
    {
        string sSql = "";
        //construct sSql based on i, dt
        base(sSql);
        //do something else
    }
}      

*/

// .    BaseClass   {       public BaseClass()       {           //       }

    public BaseClass(string sSql)
    {
        ExecSQL(sSql);
        //DO NOT do anything else
    }    

    public BaseClass(int i, DateTime dt)
    {
        //in base class ignore the parameters.
        string sWhereClause = "";
        string sSql = setSQL(sWhereClause);
        ExecSQL(sWhereClause);
        //DO NOT do anything else
    }

   protected virtual string setSQL(string value)
   {
       //set the sql
       string sSql = "";
       //add value into sSql
       return sSql;
   }

   protected virtual void ExecSQL(string sSql)
   {
       int i = 1;
       //execute query            
   }
}

public class DerivedClass: BaseClass 
{
    //public DerivedClass(string sSql):base (sSql){}
    public DerivedClass()
    {
        //do nothing
    }

    public DerivedClass(string sSql)
    {
        ExecSQL(sSql);
       //DO NOT do anything else
    }

    public DerivedClass(int i, DateTime dt)
    {
        ExecSQL(i, dt);
        //do something else after base execution
    }

   protected void ExecSQL(int i, DateTime dt) //overloaded procedure
   {
       string sWhereClause = "";
       //construct wherclause based on i, dt
       string sSql = setSQL(sWhereClause); //calls baseclass function
       base.ExecSQL(sWhereClause); //calls baseclass procedure
   }       
}

DerivedClass mdD = DerivedClass (2, DateTime.Now); BaseClass mdB = ​​ BaseClass ( "sql" );

+1

"", , ; :

  • ++.
  • , ?
  • , , "2".
0

, . , : base(), , : this() .

, .

0
source

This is a problem when the parameters that you pass to the base constructor need a validation check. This should not be done in the base constructor and cannot be done in the derived class.

(I assume that you will not have a constructor without parameters that does nothing. I cannot have this in my situation, since this requirement should not be one.)

I do not see an elegant solution.

0
source

Source: https://habr.com/ru/post/1702766/


All Articles