Skip to content

Instantly share code, notes, and snippets.

@AnthonyGiretti
Created November 23, 2025 23:29
Show Gist options
  • Select an option

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

Select an option

Save AnthonyGiretti/dcb0376635d6c0f882c29ee53e8ae144 to your computer and use it in GitHub Desktop.
C# 14: More Implicit Conversions for Span<T> and ReadOnlySpan<T>
public static class AudioAnalyzer
{
public static double AverageLevel(ReadOnlySpan<byte> data)
=> data.IsEmpty ? 0 : data.ToArray().Average(b => (double)b);
}
var bytes = track.RawData; // byte[]
// Before C# 14
AudioAnalyzer.AverageLevel(bytes.AsSpan()); // implicit conversion
AudioAnalyzer.AverageLevel(bytes[..5000].AsSpan()); // slicing also converts implicitly
// With C# 14
AudioAnalyzer.AverageLevel(bytes); // implicit conversion
AudioAnalyzer.AverageLevel(bytes[..5000]); // slicing also converts implicitly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment