Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# constructor explanation

Started by Codemaster Jamal May 25, 2019 at 10:55 PM 2 replies 2.6k views
Original Post
Codemaster Jamal
Codemaster Jamal

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);

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

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?

Nypyren
Nypyren

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.

Codemaster Jamal
Codemaster Jamal
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.