Calling base class constructor in C#
If we derive a class from a base class and want to pass data from the constructor of the derived class to the constructor of the base class, it is necessary to call base constructor .
In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below.
In the below code we declare a constructor in a derived class. We have used the ':base(...)' keyword after the constructor declaration with a specific parameter list.
The above two constructors are written to initialize base class members. The parameterless constructor of the base class is executed when we instantiate the class without passing any parameters. The other base class constructor is executed when we pass parameters while instantiating the object.
In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below.
In the below code we declare a constructor in a derived class. We have used the ':base(...)' keyword after the constructor declaration with a specific parameter list.
class Account { private string mCode; private string mName; private string mdescription; private double mBalance; public Account(string code, string name, string description, double balance) { mCode = code; mName = name; mdescription = description; mBalance = balance; } public Account() { } class PartyAccount :Account { private string mAddress; private string mPhone; public PartyAccount(string code, string name, string description, double balance, string address, string phone) : base(code, name, description, balance) { mAddress = address; mPhone = phone; } public PartyAccount() : base() { }
The above two constructors are written to initialize base class members. The parameterless constructor of the base class is executed when we instantiate the class without passing any parameters. The other base class constructor is executed when we pass parameters while instantiating the object.
Object Oriented Programming Articles
- Abstract class constructors
- abstract class inheritance in c#.net
- abstract class inheritance in visual basic.net
- New C# Language Feature: Automatic Properties
- Calling base class constructor in C#
- base keyword in c#
- Compile time polymorphism - Method OverLoading
- How to initialize Base class Object from inside Derived Class constructor
- Constructor overloading in vb.net
- How to use inheritance in c#
- How to use inheritance in visual basic.net
- Parameter array in vb.net
- Procedure OverLoading in vb.net
- Visual Inheritance with Windows Forms
- Azure Platform
- GridView CommandField example
- GridView and DetailsView Master/Detail page using SqlDataSource control
- POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
- Query entity data model using linq to entities
- Difference between arraylist and list collection
- How to create a Web service using Visual Studio.net
- FormView DataBound Event
- Calling base class constructor in C#
- Convert a sequence to a generic list using ToList()method
- Project ideas for students
- Accounting Software
- Creating an ASP.Net MVC 3 application
- Using assemblies in .net applications
- How to implement form validation using ASP.Net 2.0 Validation Controls
- Constructors in Visual Basic.Net
People also viewed:
Create your own ERP Software using ASP Core>>
Create your own Azure SOM Software>>
Create your own ERP Software using ASP .Net and SQL>>
Create your own Accounting Software using C# >>
Create your own SOM using Entity Framework MVC >>





Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net