Why does my code come up with this error? I'm trying to solve the second question on Coderbyte, it's about reversing a string.
Error:
Main.cs(8,39): error CS0411: The type arguments for method `System.MemoryExtensions.Reverse<T>(this System.Span<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
Code:
using System;
class MainClass {
public static string FirstReverse(string str) {
string backwards = new string(str.Reverse().ToArray());
return backwards;
}
static void Main() {
Console.WriteLine(FirstReverse(Console.ReadLine()));
}
}
String (https://docs.microsoft.com/en-us/dotnet/api/system.string?view=net-5.0) does not have a method Reverse, so the only thing to resolve Reverse to is a static extension method available with the using directives in your program. From System the best the compiler could do is System.MemoryExtensions.Reverse<T>(this System.Span<T>) but it couldn't use this because String is not a Span<T> for any T.
You were probably looking for Enumerable.Reverse<T> (https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.reverse?view=net-5.0) because String is an IEnumerable<char>. To use this you will need to include it with using System.Linq;