I have a generic method with this (dummy) code (yes I'm aware IList has predicates, but my code is not using IList but some other collection, anyway this is irrelevant for the question...)
static T FindThing<T>(IList collection, int id) where T : IThing, new()
{
foreach T thing in collecion
{
if (thing.Id == id)
return thing;
}
return null; // ERROR: Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead.
}
This gives me a build error
"Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead."
Can I avoid this error?
null
regardless of whetherT
isObject
orint
orchar
. – AlexanderT?
in the signature doesn't work. @Alexander-ReinstateMonica I've read the article but couldn't see a way to returnnull
regardless of reference or value type. – mireazma