CA1004: Generic methods should provide type parameter
public static void IfNullAndNullsAreIllegalThenThrow<T>(object value)
{
if (value == null && !(default(T) == null))
throw new ArgumentException("Nulls are not allowed for this object.");
}
I found this method online, which is quite useful to be honest. But, it violates the CA1004 rule. I’m not sure if there is a better way to design the method and not violate the rules.
Sample usage:
public class SomeClass<T>
{
public void SomeMethod(object obj)
{
// Ensure the actual object is not null if it shouldn't be.
ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(obj);
// ...
}
}
This looks like a helper method which is only used internally. Make it
internalinstead ofpublicand the warning should go away.