I have function like this:
private T DeserializeStream<T>(Stream data) where T : IExtensible
{
try
{
var returnObject = Serializer.Deserialize<T>(data);
return returnObject;
}
catch (Exception ex)
{
this.LoggerService.Log(this.AccountId, ex);
}
return null;
}
Everything is good, except that it complains about return null; part
Cannot convert expression type 'null' to type 'T'
How do I return null from function like this?
You can add a generic constraint that T is a class (as structures are value types and cannot be null).
private T DeserializeStream<T>(Stream data) where T : class, IExtensible
As @mindandmedia commented, an alternative is to return the default of T - this will allow it to work on value types (such a Nullable<T>, int32 etc...).
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