c# - The use of :base() in a constructor -
this question has answer here:
- c# constructor execution order 7 answers
i trying construct object derives different object, before calling base constructor make few argument validation.
public fuelmotorcycle(string owner) : base(owner) { argumentvalidation(owner); }
now understood base constructor called first, there way can call after argumentvalidation method?
now understood base constructor called first, there way can call after argumentvalidation method?
no, @ least not directly.
but can use small workaround have static
method takes argument, validates , returns if it's valid or throws exception if it's not:
private static string argumentvalidate(string owner) { if (/* validation logic owner */) return owner; else throw new yourinvalidargumentexception(); }
then have derived class constructor pass arguments through method before giving them base
constructor:
public fuelmotorcycle(string owner) : base(argumentvalidate(owner)) { }
Comments
Post a Comment