From 7783a21983450e86e3fc9b99ad0557ddcd613695 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 29 Sep 2023 14:29:09 +0200 Subject: [PATCH] reverse-string: fix span approach code --- .../reverse-string/.approaches/span/content.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/exercises/practice/reverse-string/.approaches/span/content.md b/exercises/practice/reverse-string/.approaches/span/content.md index 481116a9e8..f351f16591 100644 --- a/exercises/practice/reverse-string/.approaches/span/content.md +++ b/exercises/practice/reverse-string/.approaches/span/content.md @@ -1,12 +1,20 @@ # Span<T> ```csharp -Span chars = stackalloc[input.Length]; -for (var i = 0; i < input.Length; i++) +public static class ReverseString { - chars[input.Length - 1 - i] = input[i]; + public static string Reverse(string input) + { + Span chars = stackalloc[input.Length]; + + for (var i = 0; i < input.Length; i++) + { + chars[input.Length - 1 - i] = input[i]; + } + + return new string(chars); + } } -return new string(chars); ``` C# 7.2. introduced the [`Span`][span-t] class, which was specifically designed to allow performant iteration/mutation of _array-like_ objects.