# C# constructor explanation

## Recommended Posts

Okay, so I know what Object Parent to Child Inheritance is but, can someone explain to me what's going on in this constructor of this class?

using System;
using DarkRift.Server;

public class PluginExample : Plugin
{
public override bool ThreadSafe => false;

public override Version Version => new Version(1, 0, 0);

{

}
}

This is an example plugin I created with DarkRift Networking. I know that the class is extending the Plugin class (Meaning that the PluginExample class is a child of the Plugin class and that Plugin class is the parent class.) but, what I was trying to figure out was, what's going on with the constructor for the class?

public PluginExample(PluginLoadData pluginLoadData) : base(pluginLoadData)
{

}

I've never seen a constructor like this with other languages so I was wondering if this was a C# thing. I still got my code to work however, I was wondering what this may be exactly so perhaps I can use it to my advantage. It may be something that's only with DarkRift? Any explanation?

##### Share on other sites
Posted (edited)

When one class derives from another, the derived class's constructors are required to call one of the base class's constructors (unless there is a parameterless constructor, in which case that will be called implicitly if you don't specify a base constructor to call).  The ' : base(parameters) ' is calling the base class' constructor.

Other languages have this as well.  All OOP languages I've used have slightly different syntax to do this.  Java has "super".  C++ has initializer lists which can both initialize members and call base class constructors.

Edited by Nypyren

##### Share on other sites
4 minutes ago, Nypyren said:

When one class derives from another, the derived class's constructors are required to call one of the base class's constructors (unless there is a parameterless constructor, in which case that will be called implicitly if you don't specify a base constructor to call).  That's what the ' : base(parameters) ' means.

Oh! Ok! Thanks! I've seen this before in Java where you have to insert certain variables into the "super" of a class. Okay, I completely understand now. Thank you.

I guess because I've never looked at the Plugin class before for DarkRift that, I never saw that. Definitely want to take a look at that now just so I can see how Plugin's really work and what that parameter is for.

## Create an account

Register a new account

• 9
• 12
• 15
• 12
• 24