I have an extension method which looks like
public static T ThrowIfObjectIsNull<T>(this T argument) where T : class
{
if (argument == null)
throw new ArgumentNullException(nameof(argument));
return argument;
}
This basically check if the object that's being passed isn't null. What I am trying to do is create another extension method where the int
value which is being passed in isn't 0. So I have gone ahead and created:
public static T ThrowIfZero<T>(this T argument) where T : struct
{
if (argument == 0)
throw new ArgumentOutOfRangeException("some error here");
return argument;
}
Of course the above does not compile suggesting the error:
Error CS0019 Operator '==' cannot be applied to operands of type 'T' and 'int'
Can someone point me to the right direction on how I would check if the argument value isn't 0
?
You could simply use Equals
:
public static T ThrowIfZero<T>(this T argument) where T : struct
{
if (argument.Equals(0))
throw new ArgumentOutOfRangeException("some error here");
return argument;
}
But that will not really work well if the argument is for example a decimal 0.0m
which is not equal to the integer 0
as Peter has commented correctly.
So if you want a version that works for any number you could use this approach:
public static T ThrowIfZero<T>(this T argument) where T : struct
{
bool isZero = Decimal.Compare(0.0m, Convert.ToDecimal(argument)) == 0;
if (isZero)
throw new ArgumentOutOfRangeException("some error here");
return argument;
}
You can use EqualityComparer as well.
public static T ThrowIfZero<T>(this T argument) where T : struct
{
if (EqualityComparer<T>.Default.Equals(argument, default(T)))
throw new ArgumentOutOfRangeException("some error here");
return argument;
}
You can refer answer of this post (credit should go to @Mehrdad).
It doesn’t look like you need generics at all. If the variable is just an int as you suggest, just use:
public static int ThrowIfZero(this int argument)
{
if (argument == 0)
{
throw new ArgumentOutOfRangeException("some error here");
}
return argument;
}
int
, decimal
etc. implement IComparable
so something like this also works:
public static T ThrowIfZero<T>(this T argument)
where T : struct, IComparable
{
if (argument.CompareTo(default(T)) == 0)
throw new ArgumentOutOfRangeException("some error here");
return argument;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With