- Home /
Checking if variable is nullable type?
How do I check, not if a variable is null, but if it's a nullable type?
I want to make an extension function for a null comparison that applies to every variable that's possibly nullable, so if I have something like
BoxCollider col;
I can just check if it's null with
col.IsNull();
However, if I check for null on say an int or float, that'll throw an error since ints are non-nullable, closest would be some IsNan stuff. How can I make a general IsNull check across all potentially nullable variables, and further, how do I know inherently if a variable is of a nullable type? Thanks!
Answer by nyonge · Jan 29, 2020 at 09:05 PM
Think I figured it out, will post for reference for anyone else. Nullable doesn't apply to basic .net data types like int or float, but is a property of all UnityEngine objects like Vectors and MonoBehaviours.
I made some extension methods to check if a single variable is null, or if two objects are mismatched null types (one null, the other not):
/// <summary>
/// Is this object null or unassigned? Returns true if so
/// </summary>
/// <param name="o">this object to check</param>
/// <returns>true if null</returns>
public static bool IsNull(this Object o) {
if (safeNullCheck && (!o || o == null))
return true;
return !o;
}
private const bool safeNullCheck = false;
/// <summary>
/// Returns true if one object's IsNull state does not match the other's
/// </summary>
/// <param name="o">this object to check</param>
/// <param name="obj">Object to compare against this one</param>
/// <returns>true if objects null states do not match</returns>
public static bool IsNullMismatched(this Object o, Object obj) {
if (o.IsNull ()) {
if (obj.IsNull ())
return false;
return true;
}
return !obj.IsNull ();
}
/// <summary>
/// Returns true if one object's IsNull state does not match the other's. Outputs a bool specifiying if both objects are null.
/// </summary>
/// <param name="o">this object to check</param>
/// <param name="obj">Object to compare against this one</param>
/// <param name="areBothNull">True if both objects are null, false if either is non-null</param>
/// <returns>true if objects null states do not match</returns>
public static bool IsNullMismatched(this Object o, Object obj, out bool areBothNull) {
if (o.IsNull ()) {
if (obj.IsNull ()) {
areBothNull = true;
return false;
}
areBothNull = false;
return true;
}
areBothNull = false;
return !obj.IsNull ();
}
Which should cover what I'm looking for, and should work for all data types that extend from UnityEngine.Object. Props to Jovanni Cutigni for going into more detail about nullable info on their site. (archived)
Answer by Kishotta · Jan 29, 2020 at 09:17 PM
According to this StackOverflow question, you could use an extension method to make this easily accessible.
public static class NullableExtensions {
public static bool IsNull<T> (this T obj) {
if (obj == null) return true; // obvious
Type type = typeof (T);
if (!type.IsValueType) return true; // reference type
if (Nullable.GetUnderlyingType (type) != null) return true; // Nullable<T>
return false; // value type
}
}
Usage:
private void Awake () {
if (_boxCollider.IsNull ()) {
// _boxCollider is both nullable and is actually null
}
}
Answer by sacredgeometry · Feb 02, 2020 at 01:13 PM
Like these?
default(Type) == null
!typeof(Type).isValueType
or this
!myObj.GetType().isValueType