19
The constructor does return a value - the type being constructed...
A constructor is not supposed to return any other type of value.
When you validate in a constructor, you should throw exceptions if the passed in values are invalid.
public class MyType
{
public MyType(int toValidate)
{
if (toValidate < 0)
{
throw new ArgumentException("toValidate should be positive!");
}
}
}
A constructor returns the type being instantiated, and, if necessary, can throw an exception. Perhaps a better solution would be to add a static method to try creating an instance of your class:
public static bool TryCreatingMyClass(out MyClass result, out string message)
{
// Set the value of result and message, return true if success, false if failure
}