Dummy code:
// TSource1 elementObj can be struct or can be class object and never can be nullable struct or null (because method inner code will create Exceptions in that case)
public static TSource1? ReturnNullOrNotNullTSource1<TSource1>(TSource1 elementObj)
{
// making some checkings for elementObj, if chekings have passed I need to return that elementObj back, if chekings have not passed I need to return null (so I used TSource1? as a method return type).
// ...
return (passed) ? elementObj : default(TSource1?);
}
I can return default(TSource1?) with a case when checkings have not passed and that code is compiled, but when I test it with TSource1=DateOnly I see that the actual object that was returned is not null but '01.01.0001'. It returns default(TSource1) instead default(TSource1?) and it is incorrect for me because I can't know have checkings not passed or they have passed for this case:
var result = ReturnNullOrNotNullTSource1(new DateOnly(0001, 1, 1));
I expect I can return nullable struct T for incoming non-nullable struct T but that code works wrong. What can I do?