C# Nullable Pitfalls: When T? Isn't What You Think
2025-08-29
C#'s reuse of `T?` syntax for both nullable value types and nullable reference types creates confusion. For value types, `T?` is syntactic sugar for `Nullable`, representing distinct types. However, for reference types, `T?` is merely an intent marker; after compilation, `T?` and `T` are the same type. This difference leads to compilation errors when writing generic methods. The article demonstrates this issue with a `SelectNotNull` method mimicking F#'s `List.choose`. The solution involves method overloading with type constraints (`where TR : class` and `where TR : struct`) to disambiguate value and reference types. While the problem is solved, the design remains inelegant.
Read more
Development
Nullable Types