3

I am currently trying to construct an object which derives from a different object, but before calling the base constructor I would like to make a few argument validation.

public FuelMotorCycle(string owner) : base(owner)
{
    argumentValidation(owner);
}

Now I understood that initially the base constructor is called first, is there a way I can call it only after the argumentValidation method?

svick
  • 236,525
  • 50
  • 385
  • 514
Steinfeld
  • 649
  • 2
  • 10
  • 20
  • 2
    `base` construction will occur first. – Matthew Watson May 18 '13 at 12:52
  • 4
    You could always press F5 and see for yourself. – Bob Horn May 18 '13 at 12:54
  • 1
    Duplicate of: [C# constructor execution order](http://stackoverflow.com/a/1882778/1945631) – Andy Brown May 18 '13 at 12:56
  • 2
    No, you can't easily do that. (There are sneaky tricks, as some people have mentioned.) But what does it matter? If the arguments are invalid for the base constructor, it will throw. If they're not invalid for the base constructor but are invalid for the derived constructor, it will throw. Either way, it throws. And since a program which causes a ctor to throw is *buggy*, it will never ship to customers, so what does it matter if the throw happens earlier or later? – Eric Lippert May 18 '13 at 14:33

5 Answers5

5

The base constructor will be invoked first.

This example:

class Program
{
    static void Main(string[] args)
    {
        var dc = new DerivedClass();
        System.Console.Read();
    }
}

class BaseClass
{
    public BaseClass(){
        System.Console.WriteLine("base");
    }
}

class DerivedClass : BaseClass
{
    public DerivedClass()
        : base()
    {
        System.Console.WriteLine("derived");
    }
}

will output:

base
derived
Thorkil Holm-Jacobsen
  • 7,287
  • 5
  • 30
  • 43
4

Now I understood that initially the base constructor is called first, is there a way I can call it only after the argumentValidation method?

No, at least not very directly.

But you can use a small workaround where you have static method that takes the argument, validates it and returns it if it's valid or throws an exception if it's not:

private static string argumentValidate(string owner)
{
    if (/* validation logic for owner */) return owner;
    else throw new YourInvalidArgumentException();
}

Then have the derived class constructor pass the arguments through this method before giving them to the base constructor:

public FuelMotorCycle(string owner) : base(argumentValidate(owner))
{
}
svick
  • 236,525
  • 50
  • 385
  • 514
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
2

The base class constructor will be called first, then everything in the method body will be executed.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
1

Base is used in constructors. A derived class constructor is required to call the constructor from its base class. When the default constructor isn't present, the custom base constructor can, with base, be referenced.Base class constructors get called before derived class constructors, but derived class initializers get called before base class initializers.I also suggest you to see the contructor execution from msdn

using System;

public class A // This is the base class.
{
    public A(int value)
    {
    // Executes some code in the constructor.
    Console.WriteLine("Base constructor A()");
    }
}

public class B : A // This class derives from the previous class.
{
    public B(int value)
    : base(value)
    {
    // The base constructor is called first.
    // ... Then this code is executed.
    Console.WriteLine("Derived constructor B()");
    }
}

class Program
{
    static void Main()
    {
    // Create a new instance of class A, which is the base class.
    // ... Then create an instance of B, which executes the base constructor.
    A a = new A(0);
    B b = new B(1);
    }
}

Here is the output:

Base constructor A()
Base constructor A()
Derived constructor B()
Freak
  • 6,786
  • 5
  • 36
  • 54
1

Depending on what the validation method is, you may be able to do something like:

public FuelMotorCycle(string owner) 
    : base(argumentValidationReturningValidatedArg(owner))
{
}

If the validation function is a static method, for example, this should be fine.

Chris Dickson
  • 11,964
  • 1
  • 39
  • 60