🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# constructor explanation

Started by
2 comments, last by Codemaster Jamal 5 years, 1 month ago

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?

Advertisement

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.

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.

This topic is closed to new replies.

Advertisement