Allow Default Value Declaration for Nullable Properties in DTOs #9551
Replies: 3 comments 11 replies
-
Isn't this just:
|
Beta Was this translation helpful? Give feedback.
-
The problem I've got with both public string? Name { get; set; default ""; } and public string? Name { get => field ?? ""; set; } is that neither of them are |
Beta Was this translation helpful? Give feedback.
-
The database results have nulls, but you really don't want to deal with those nulls in your DTO objects or the code that uses them, so you want to translate the nulls to some other values. You are sort of mapping between domains here, but you want that mapping to be some intrinsic feature that C# or dotnet does for you. It has problems to be a C# feature. Either the property itself is declared to accept null and the automatic mapping would intercept your ability to actual have null which would seem bad, or the property does not claim to allow null and the language would do some auto conversion from null to the property specific default value, which is not as bad but introduces some new non-intuitive magic. It seems this is better suited for a feature of the tools you are already using (like the serializer) and represented as a custom attribute on your type in a similar way that those tools use already. You might not get any automatic benefit in code you are writing out by hand, but any tool constructing your object would benefit by having a specific default to use when attempting to assign null to a property that does not accept null. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
In data-transfer-heavy applications (especially when interacting with databases), developers often need to assign fallback values when reading potentially null fields (e.g., from IDataReader, NpgsqlDataReader, etc.).
C# currently requires either:
1.Manually checking for DBNull on every read.
2.Using nullable fields with wrapper properties for fallback.
3.Null-coalescing logic in constructors or mapping code.
While these approaches work, they add verbosity and complexity, especially in large DTO models.
Proposal
Introduce
syntax that allows defining default fallback values directly in nullable property definitions.Suggested Syntax (for discussion):
Or alternatively (using new property syntax):
Behavior
1.When a null value is assigned (via deserialization, manual mapping, or reflection), the defined default will be used automatically.
2.This avoids the need for explicit ?? checks or backing properties.
3.Only applies when the value is null, not when a valid non-null value is assigned.
Motivation
Today, developers often write repetitive null-handling logic like:
Or use verbose wrappers:
This adds clutter and reduces code readability.
Benefits
1.Cleaner, more expressive DTOs.
2.Less repetitive null/DBNull checking.
3.Faster development for data-driven applications.
4.Easier deserialization from JSON/XML/db sources where nulls are common.
Considerations
1.Backward compatibility: Safe if the default fallback is optional.
2.Compiler behavior: Should ensure fallback occurs only if value is null, not when explicitly set.
Prior Art & Related Discussions
I searched existing issues and language design notes but couldn’t find a similar feature proposed or planned — apologies if I missed one.
If this overlaps with something already discussed (like auto-backing fields or property initializers), happy to learn and adapt this idea accordingly.
Closing Thoughts
This feature would enhance productivity for developers working with DTOs and bridge the gap between data access and clean domain modeling. Open to feedback, syntax suggestions, or technical concerns from the community and LDM team.
Thanks for considering it!
Beta Was this translation helpful? Give feedback.
All reactions