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

What is the meaning of " : base " in the business class of the next class (MyClass)? Please explain the concept of constructor definition below for the MyClass class.

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; } } 
+6
constructor c #
source share
4 answers

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

When writing base(data) you instruct the program to use a single WorkerThread constructor that takes data as a parameter. If you did not do this, the program will try to use the default constructor - one that can be called without parameters.

+18
source share

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

Regarding call

 new WorkerThread(data) 
+2
source share

This means that you pass the data parameter passed to the MyClass constructor to the base class constructor (WorkerThread), which actually calls

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

A rare case where VB can be clearer ...

 Public Class MyClass Inherits WorkerThread Public Sub New(data) MyBase.New(data) End Sub End Class 
-one
source share

All Articles