Call base class constructor using base() method

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 4365 red flag

How to call base class constructor using base() method

Call base class constructor using base() method

In this article we will learn how to call base class constructor using base() method. To show in example we will create two classes. In out example Person is base class and GoodPerson is derive class. From derive class constructor we will call base class constructor using base() method.

Call constructor of base class from derive class without argument

Create Person and GoodPerson class at first like below. From derive class constructor we are calling base class constructor using base() method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    public class Person
    {
        public String PersonName { get; set;}
        public String PersonSurname { get; set;}
        public Person()
        {
            this.PersonName = "Sourav";
            this.PersonSurname = "Kayal";
        }
    }
    public class GoodPerson:Person
    {
        public int Rating { get; set; }
        public GoodPerson():base()
        {
Rating = 5;
        }
    }
 
}
 
Create client code

Create one window from and in Button’s click event write below code

private void button1_Click(object sender, EventArgs e)
{
           
            GoodPerson P1 = new GoodPerson();
            this.lblName.Text = P1.PersonName;
            this.lblSurname.Text = P1.PersonSurname;
            this.lblRating.Text = P1.Rating.ToString();
}

Here is sample user interface


When we will click on button it will set default value (within constructor of both base and derive class) of class object. And the value will get display in user interface.


Call base class constructor with argument

Now, we will see how to pass parameter to base class constructor. We can pass parameter just like function parameter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    public class Person
    {
        public String PersonName { get; set;}
        public StringPersonSurname { get; set;}
        public Person()
        {
        }
        public Person(String Name,String Surname)
        {
            this.PersonName = Name; ;
            this.PersonSurname = Surname;
        }
       
    }
    public class GoodPerson:Person
    {
        public int Rating { get; set; }
        public GoodPerson()
        {
 
        }
        public GoodPerson(GoodPerson P) :base(P.PersonName,P.PersonSurname)
        {
            this.Rating = P.Rating;
        }
    }
 
}

From derive class constructor we are passing two arguments called PersonName and PersonSurname to base class’s constructor.


Create client code

Now we will send data from buttons click event.

        private void button1_Click(object sender, EventArgs e)
        {
            GoodPerson P = new GoodPerson();
P.PersonName = "TestName";
P.PersonSurname = "TestSurname";
P.Rating = 5;
           GoodPerson P1 = new GoodPerson(P);
            this.lblName.Text = P1.PersonName;
            this.lblSurname.Text = P1.PersonSurname;
            this.lblRating.Text = P1.Rating.ToString();
        }
    }
 

Here is sample output.


Conclusion:- 

Here we have learned how to call base class constructor using base() function. 



Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)