英文:
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("API KEY");
DetectResult[] results = client.DetectAsync(sourceText);
[...]
using the https://detectlanguage.com/ API
答案1
得分: 0
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);
}
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论