Jump to content
  • Advertisement
Codemaster Jamal

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

    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?

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!