Created
November 23, 2025 23:29
-
-
Save AnthonyGiretti/dcb0376635d6c0f882c29ee53e8ae144 to your computer and use it in GitHub Desktop.
C# 14: More Implicit Conversions for Span<T> and ReadOnlySpan<T>
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
| 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