this post was submitted on 16 Jun 2023
2 points (100.0% liked)
C Sharp
1526 readers
1 users here now
A community about the C# programming language
Getting started
Useful resources
- C# documentation
- C# Language Reference
- C# Programming Guide
- C# Coding Conventions
- .NET Framework Reference Source Code
IDEs and code editors
- Visual Studio (Windows/Mac)
- Rider (Windows/Mac/Linux)
- Visual Studio Code (Windows/Mac/Linux)
Tools
Rules
- Rule 1: Follow Lemmy rules
- Rule 2: Be excellent to each other, no hostility towards users for any reason
- Rule 3: No spam of tools/companies/advertisements
Related communities
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I really like nullable types, they can be very effective for writing safer code.
Sometimes there are good reasons to separate object construction and initialization (e.g. composite / loosely coupled objects, or encapsulation of 3rd party libraries) so there can be properties/fields that do not yet have valid values, and using separate queries for this is error prone.
I write a lot of communication interfaces for sensors/actuators and if the communication drops, nullables are a good way to represent invalid readings.
Being able to convey the value and validity in one variable can be more thread-safe and easier to write pure-functions, and show intent.
I occasionally use a nullable for singleton patterns if Iโm not 100% convinced there can never be multiple instances, rather than painting myself into a corner with a static class. e.g.
Those who are trying to avoid milk should consider using
Lazy<MyClass> _instance;