Base constructor in C# – Which gets called first? [duplicate]

Base class constructors get called before derived class constructors, but derived class initializers get called before base class initializers. E.g. in the following code:

public class BaseClass {

    private string sentenceOne = null;  // A

    public BaseClass() {
        sentenceOne = "The quick brown fox";  // B
    }
}

public class SubClass : BaseClass {

    private string sentenceTwo = null; // C

    public SubClass() {
        sentenceTwo = "jumps over the lazy dog"; // D
    }
}

Order of execution is: C, A, B, D.

Check out these 2 msdn articles:

  • Why do initializers run in the opposite order as constructors? Part One
  • Why do initializers run in the opposite order as constructors? Part Two

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)