base Keyword

This type of constructor will call BaseClass.BaseClass
If you omit the call to the base constructor it will be called automatically
If there is no default (parameteless) base constructor then one must be called explicitly
The constructor for the base class is called before the block for the constructor is executed

public class DerivedClass : BaseClass 
{
   public DerivedClass() : base()
   {
   }
}

This is the same as

public class DerivedClass : BaseClass 
{
   public DerivedClass()
}

You can also use this to pass items between the derived and the base classes
This type of constructor will call BaseClass.BaseClass(string)

public class DerivedClass : BaseClass 
{
   public DerivedClass(string message)
   {
      base(message);
   }
}

vs

public class DerivedClass : BaseClass 
{
   public DerivedClass(string message) : base(message)
   {
   }
}

The base keyword can be used with or without parameters
Use base when there is inheritance and a parent class already provides the same arguments



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext