I try to limitate my methods so that some input arguments only allow numbers greater than zero. I thought about creating my own class, but generating an object for each method call seems to be ineffective to me. Silly example method:
public class Whatever
{
public uint Num1 { get; }
public double Num2 { get; }
public Whatever (uint num1, double num2) {
Num1 = num1;
Num2 = num2;
}
public double SillyDivide() {
var result1 = Num1 / Num2;
var result2 = Num2 / Num1;
return result1 + result2;
}
}
Obviously, I try to avoid division by zero, by only allowing number higher than zero, or not equal to zero. What is the best way to implement this?
I found a Q&A here and tried to implement it, but it did not make any difference:
public class Whatever
{
[Required]
[Range(1, uint.MaxValue)]
public uint Num1 { get; }
[Required]
[Range(double.Epsilon, double.MaxValue)]
public double Num2 { get; }
public Whatever (uint num1, double num2) {
Num1 = num1;
Num2 = num2;
}
public double SillyDivide() {
var result1 = Num1 / Num2;
var result2 = Num2 / Num1;
return result1 + result2;
}
}