Two options:
default(T)
which means you'll return null
if T is a reference type (or a nullable value type), 0
for int
, '\0'
for char
, etc. (Default values table (C# Reference))where T : class
constraint and then return null
as normalreturn default(T);
You can just adjust your constraints:
where T : class
Then returning null is allowed.
Add the class constraint as the first constraint to your generic type.
static T FindThing<T>(IList collection, int id) where T : class, IThing, new()
If you have object then need to typecast
return (T)(object)(employee);
if you need to return null.
return default(T);
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