Last active
November 24, 2025 17:26
-
-
Save AnthonyGiretti/9ebed0f136281177c6b243a7af0d911b to your computer and use it in GitHub Desktop.
Before C# 14 and with C# 14 null conditional assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Not allowed before C# 14, does not compile | |
| track?.Info = new Metadata(); | |
| track?.Info?.Duration = TimeSpan.Zero; | |
| playlist?["title"] = "My Mix"; | |
| // Must do this | |
| if (track != null && track.Info != null) | |
| { | |
| track.Info.Duration = TimeSpan.FromMinutes(5); | |
| } | |
| // Allowed with C# 14 | |
| // Update nested properties | |
| track.Info?.Duration = TimeSpan.FromMinutes(3.5); | |
| // Update dictionaries | |
| track.Info?.Tags?["Genre"] = "Jazz"; | |
| // Nullable arrays | |
| string[]? comments = null; | |
| comments?[0] = "Great intro!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment