When working with local variables, a value must always be assigned before accessing them.
The reason for this is that usually, when a developer omits initialization, he relies on the runtime to give it a default value, but if he forgot, this can cause some unnecessary errors.
In the case of working with generics and not specifying whether you expect a ReferenceType or ValueType , you cannot just initialize it by assigning null . In this case, you need to use the default keyword .
This initializes the null variable for reference types or assigns 0 for numeric value types. For structs, it initializes each member by default.
In the above example, comparison with null allows me to assume that this method can only be used for ReferenceTypes, if so, it is better to add a class constraint.
Yannick motton
source share