<p>In C#, if I have an inherited class with a default constructor, do I have to explicitly call the base class' constructor or will it be implicitly called?</p>

<pre><code>class BaseClass
{
    public BaseClass()
    {
        // ... some code
    }
}
class MyClass : BaseClass
{
    public MyClass() // Do I need to put ": base()" here or is it implied?
    {
        // ... some code
    }
}
</code></pre>

[{'answer_id': 18100, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 0, 'selected': False, 'text': '<p>AFAIK, you only need to call the base constructor if you need to pass down any values to it.</p>\n'}, {'answer_id': 18102, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 3, 'selected': False, 'text': '<p>It is implied.</p>\n'}, {'answer_id': 18109, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 7, 'selected': True, 'text': '<p>You do not need to explicitly call the base constructor, it will be implicitly called.</p>\n\n<p>Extend your example a little and create a Console Application and you can verify this behaviour for yourself:</p>\n\n<pre><code>using System;\n\nnamespace ConsoleApplication1\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            MyClass foo = new MyClass();\n\n            Console.ReadLine();\n        }\n    }\n\n    class BaseClass\n    {\n        public BaseClass()\n        {\n            Console.WriteLine("BaseClass constructor called.");\n        }\n    }\n\n    class MyClass : BaseClass\n    {\n        public MyClass()\n        {\n            Console.WriteLine("MyClass constructor called.");\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 18115, 'author': 'Tom Welch', 'author_id': 1188, 'author_profile': 'https://Stackoverflow.com/users/1188', 'pm_score': 3, 'selected': False, 'text': '<p>A derived class is built upon the base class. If you think about it, the base object has to be instantiated in memory before the derived class can be appended to it. So the base object will be created on the way to creating the derived object. So no, you do not call the constructor.</p>\n'}, {'answer_id': 18170, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 5, 'selected': False, 'text': "<p>It is implied, provided it is parameterless. This is because you <strong>need to implement constructors that take values</strong>, see the code below for an example:</p>\n\n<pre><code>public class SuperClassEmptyCtor\n{\n    public SuperClassEmptyCtor()\n    {\n        // Default Ctor\n    }\n}\n\npublic class SubClassA : SuperClassEmptyCtor\n{\n    // No Ctor's this is fine since we have\n    // a default (empty ctor in the base)\n}\n\npublic class SuperClassCtor\n{\n    public SuperClassCtor(string value)\n    {\n        // Default Ctor\n    }\n}\n\npublic class SubClassB : SuperClassCtor\n{\n    // This fails because we need to satisfy\n    // the ctor for the base class.\n}\n\npublic class SubClassC : SuperClassCtor\n{\n    public SubClassC(string value) : base(value)\n    {\n        // make it easy and pipe the params\n        // straight to the base!\n    }\n}\n</code></pre>\n"}, {'answer_id': 18185, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s implied for base parameterless constructors, but it is needed for defaults in the current class:</p>\n\n<pre><code>public class BaseClass {\n    protected string X;\n\n    public BaseClass() {\n        this.X = "Foo";\n    }\n}\n\npublic class MyClass : BaseClass\n{\n    public MyClass() \n        // no ref to base needed\n    {\n        // initialise stuff\n        this.X = "bar";\n    }\n\n    public MyClass(int param1, string param2)\n        :this() // This is needed to hit the parameterless ..ctor\n    {\n         // this.X will be "bar"\n    }\n\n    public MyClass(string param1, int param2)\n        // :base() // can be implied\n    {\n         // this.X will be "foo"\n    }\n}\n</code></pre>\n'}, {'answer_id': 18196, 'author': 'jl23x', 'author_id': 2143, 'author_profile': 'https://Stackoverflow.com/users/2143', 'pm_score': -1, 'selected': False, 'text': '<p>You don’t need call the base constructor explicitly it will be implicitly called, but sometimes you need pass parameters to the constructor in that case you can do something like:</p>\n\n<pre><code>using System;\nnamespace StackOverflow.Examples\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            NewClass foo = new NewClass("parameter1","parameter2");\n            Console.WriteLine(foo.GetUpperParameter());\n            Console.ReadKey();\n        }\n    }\n\n    interface IClass\n    {\n        string GetUpperParameter();\n    }\n\n    class BaseClass : IClass\n    {\n        private string parameter;\n        public BaseClass (string someParameter)\n        {\n            this.parameter = someParameter;\n        }\n\n        public string GetUpperParameter()\n        {\n            return this.parameter.ToUpper();\n        }\n    }\n\n    class NewClass : IClass\n    {\n        private BaseClass internalClass;\n        private string newParameter;\n\n        public NewClass (string someParameter, string newParameter)\n        {\n            this.internalClass = new BaseClass(someParameter);\n            this.newParameter = newParameter;\n        }\n\n        public string GetUpperParameter()\n        {\n            return this.internalClass.GetUpperParameter() + this.newParameter.ToUpper();\n        }\n    }\n}\n</code></pre>\n\n<p>Note: If someone knows a better solution please tells me.</p>\n'}]
['https://Stackoverflow.com/questions/18097', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']