Why `where T: class`
I am reading a book on c# 12. In this example the author includes
where T: class
but why is that needed?
It seems to work fine without5 Replies
To not accept structs
generics are a bit finnicky with how they deal with
null
. Im sure you are aware that a struct can't be null, so a bool?
can't actually be null, its instead handled by a wrapper called Nullable<T>
you can look up exactly how it works, but due to structs not being able to be null, there are generic constraints like class
, struct
, notnull
and class?
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters for more infothank you @ZZZZZZZZZZZZZZZZZZZZZZZZZ @Pobiega
Because they already rejected
structnt
as a constraint name