they are written is a separate class because those methods are utility methods and are intended to be used anywhere without actually having to create a nullable
structure instance every time you want to use these methods for example,
public static bool Is<T>(this T variable,Type type) {
if (var != null) {
Type currentType = variable.GetType();
type = Nullable.GetUnderlyingType(type) ?? type;
while (currentType != typeof(object)) {
if (currentType == type) {
return true;
}
currentType = currentType.BaseType;
}
}
return false;
}
In the above example the variable can be of any type(not necessarily nullable
), but it can actually be nullable
, and hence has to be checked if nullable
.
For that very case I used the Nullable.GetUnderlyingType(type)
, because it is a utility method.This is intended behaviour for utility methods i.e. making them reusable anywhere required without having to create an instance, every time you need them.