Skip to content

Instantly share code, notes, and snippets.

@AnthonyGiretti
Last active November 24, 2025 17:26
Show Gist options
  • Select an option

  • Save AnthonyGiretti/9ebed0f136281177c6b243a7af0d911b to your computer and use it in GitHub Desktop.

Select an option

Save AnthonyGiretti/9ebed0f136281177c6b243a7af0d911b to your computer and use it in GitHub Desktop.
Before C# 14 and with C# 14 null conditional assignment
// 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