I am trying to extend a program with a language code detection but I get this Error when i try to implement it.
Error CS0029 Cannot implicitly convert type ‘System.Threading.Tasks.Task<DetectLanguage.DetectResult[]>’ to ‘DetectLanguage.DetectResult[]’
private static Translation Translate(string sourceText, string lang)
{
DetectLanguageClient client = new DetectLanguageClient("API KEY");
DetectResult[] results = client.DetectAsync(sourceText);
[...]
using the https://detectlanguage.com/ API
>Solution :
The API that you’re calling returns a Task<T>, which is essentially an async operation – not the result of such.
To get the actual result of the async operation, you need to use await. Since await can only be used inside an async function, you also need to declare your Translate function as such;
private static async Task<Translation> Translate(string sourceText, string lang)
{
DetectLanguageClient client = new DetectLanguageClient("API KEY");
DetectResult[] results = await client.DetectAsync(sourceText);