Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the purpose of calling a base constructor when creating a constructor for a derived class?

Example:


C#
class Manager : Employee
    {
        public Manager()
            :base()
        {
            
        }
Posted
Updated 15-Feb-14 7:55am
v2

A base constructor allows common initialization code to be executed when a derived class is constructed.  You explicitly call a base constructor when you want to pass arguments to it when the derived class is being constructed.  For example:
C#
public class MyException : Exception
{
    public MyException
        (string message,
         object data) : base (message)
    {
        // Common initialization comes here
        ...
    }
}
/ravi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-14 22:48pm    
Correct, a 5.
However, you should have mentioned that in OP's code base() is totally redundant and would be automatically called.
—SA
Ravi Bhavnani 15-Feb-14 23:01pm    
You're absolutely right, Sergey.

/ravi
In your example none. In this case Employee's constructor will automatically be called.

However, if you have a constructor with parameters, there is a purpose. Consider the following example:

C#
abstract class Foo
{
    protected Foo()
    {
    }

    protected Foo(int number)
    {
    }

    protected Foo(string s)
    {
    }
}

class Bar : Foo
{
    public Bar()
    {
        // Foo() is invoked automatically, since the call to the base constructor is omitted.
    }

    public Bar(int number)
        : base(number)
    {
        // Foo(int) is called, since it is explicitly invoked
    }

    public Bar(string s)
    {
        // Foo() is invoked automatically, since the call to the base constructor is omitted.
    }
}


If Foo does not have a default constructor (in my example it does), its constructor needs to be explicitly called. There will actually be a compiler error if you don't implement some constructor on Bar, and if you don't call the base constructor, there will be an error as well.

Bottom line, if you have a parameterless constructor in your base class, it will be automatically called if you don't explicitly specify it. And if you do not have a parameterless constructor, you have to call it from your derived class whether you want it or not.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Feb-14 22:50pm    
Correct, a 5. I would explicitly mention that in OP's code base() is totally redundant due to the reason you already explained.
—SA
base constructor is used for run base class methods
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-14 14:08pm    
Really? How interesting! Then please tell us: is it possible to call base class methods without the base constructor?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900