Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of ": base" in the constructor definition?

What is the meaning of ": base" in the costructor of following class(MyClass) ? Please explain the concept behind constructor definition given below for class MyClass.

public class MyClass: WorkerThread
{
        public MyClass(object data): base(data) 
        { 
           // some code       

        }
}

public abstract class WorkerThread
{

        private object ThreadData;
        private Thread thisThread;

        public WorkerThread(object data)
        {
            this.ThreadData = data;
        }

        public WorkerThread()
        {
            ThreadData = null;
        }
}
like image 483
DotNetBeginner Avatar asked Mar 29 '10 09:03

DotNetBeginner


2 Answers

The base class is WorkerThread. When you create a MyClass, a WorkerThread must be created, using any of its constructors.

By writing base(data) you are instructing the program to use one WorkerThread's constructor which takes data as a parameter. If you didn't do this, the program would try to use a default constructor - one which can be called with no parameters.

like image 162
Daniel Daranas Avatar answered Sep 21 '22 18:09

Daniel Daranas


It calls the constructor of the class it inherits from, and provides the according arguments.

Sort of like calling

new WorkerThread(data)
like image 34
Nikos Steiakakis Avatar answered Sep 23 '22 18:09

Nikos Steiakakis