How can i determine if value of generic method is null? So only way i found is check that it's a class and it has default value. So my code:
public static string AsJsonArray<T>(this IEnumerable<T> collection)
{
var sb = new StringBuilder("[");
var enumerator = collection.GetEnumerator();
bool isClass = typeof (T).IsClass;
if (enumerator.MoveNext())
{
if (isClass && enumerator.Current == default(T))
sb.Append("null");
else
sb.Append(enumerator.Current);
while (enumerator.MoveNext())
{
sb.Append(", ");
if (isClass && enumerator.Current == default(T))
sb.Append("null");
else
sb.Append(enumerator.Current);
}
}
var asJsonArray = sb.Append("]").ToString();
return asJsonArray;
}
but i'm really annoyed by this ugly reflection check isClass && enumerator.Current == default(T)
Do any alternative exist?
As comments suggest, use the ==
operator or a ReferenceEquals
check.
private static bool IsNull<T>(T item)
{
return object.ReferenceEquals(null, item);
}
This yields:
int? nullableInt = null;
Console.WriteLine(IsNull(nullableInt)); //true
object refType = null;
Console.WriteLine(IsNull(refType)); //true
int valueType = 0;
Console.WriteLine(IsNull(valueType)); //false
The IsClass
check you do would fail for the Nullable<T>
because Nullable
is actually a struct
.
To remove duplicate code you could even create a method to check for null and replace with text:
private static string ToStringOrDefault<T>(T item, string replacement = "null")
{
return IsNull(item) ? replacement : item.ToString();
}
sb.Append(ToStringOrDefault(nullableInt));
It sounds like you just need == null
:
var current = enumerator.Current;
if (current == null)
{
sb.Append("null");
}
else
{
sb.Append(current);
}
Or more compactly:
var current = enumerator.Current;
sb.Append(current == null ? (object) "null" : current);
Or even by boxing before the call, and using the null-coalescing operator:
object current = enumerator.Current; // Boxes where necessary
sb.Append(current ?? "null");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.