Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<DetectLanguage.DetectResult[]>' to 'DetectLanguage.DetectResult[]'

huangapple go评论55阅读模式
英文:

Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<DetectLanguage.DetectResult[]>' to 'DetectLanguage.DetectResult[]'

问题

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[]'

{
    DetectLanguageClient client = new DetectLanguageClient("API KEY");
    DetectResult[] results = client.DetectAsync(sourceText);

[...]

Using the https://detectlanguage.com/ API.

英文:

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(&quot;API KEY&quot;);
            DetectResult[] results = client.DetectAsync(sourceText);

[...]

using the https://detectlanguage.com/ API

答案1

得分: 0

The API that you're calling returns a Task&lt;T&gt;, 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&lt;Translation&gt; Translate(string sourceText, string lang)
{
    DetectLanguageClient client = new DetectLanguageClient(&quot;API KEY&quot;);
    DetectResult[] results = await client.DetectAsync(sourceText);
}
英文:

The API that you're calling returns a Task&lt;T&gt;, 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&lt;Translation&gt; Translate(string sourceText, string lang)
        {
            DetectLanguageClient client = new DetectLanguageClient(&quot;API KEY&quot;);
            DetectResult[] results = await client.DetectAsync(sourceText);

huangapple
  • 本文由 发表于 2023年5月8日 01:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定