I’m inheriting from a ListBox. Do I need to explicitly call the base constructor?
public class MyListBox : ListBox
{
public MyListBox() : base()
{
}
// or
public MyListBox()
{
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The two options you listed will compile to identical code.
The only reason to include
: base()is to explicitly denote that you are calling the base classes default constructor instead of some other constructor, by design. If left off, this happens automatically. As such, this is completely optional.However, if you want to use a constructor other than the parameterless constructor of the base class, you would have to explicitly state this, ie:
This would explicitly use a constructor which accepted a string as an argument.