C# 无法将类型“int”隐式转换为“...Tasks<int>”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15462004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:54:48  来源:igfitidea点击:

Cannot implicitly convert type 'int' to '...Tasks<int>'

c#task

提问by ploxtic

if this is async, it'll return with no error, why is it throwing an error without being async, async is worthless in this operation.

如果这是异步的,它将无错误地返回,为什么它在没有异步的情况下抛出错误,异步在此操作中毫无价值。

public Task<int> countUp()
{
    string compare = txtTag.Text;
    int count = 0;
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (compare == dataGridView1[0, i].Value.ToString())
        {
            BeginInvoke(new Action(() =>
            {
                count++;
                txtCount.Text = count.ToString();
            }));
        }
    }

    return count;
}

回答by SLaks

As the error clearly states, you can't return an intas a Task<int>. (unless you make it an asyncmethod, which does compile-time magic to create a Task<T>.

正如错误明确指出的那样,您不能将 anint作为 a返回Task<int>。(除非你把它变成一个async方法,它会在编译时神奇地创建一个Task<T>.

If your method isn't asynchronous, you shouldn't be returning a Task<T>in the first place.
Instead, just return intdirectly.

如果您的方法不是异步的,那么您一开始就不应该返回 a Task<T>
相反,只需int直接返回。

If, for some reason, you needto return a Task<T>, you can call Task.FromResult()to create a finished task with a given value.

如果由于某种原因需要返回 a Task<T>,则可以调用Task.FromResult()以创建具有给定值的已完成任务。

回答by spender

Well, you couldreturn a completed Task:

好吧,你可以返回一个完成的任务:

return Task.FromResult(count);

http://msdn.microsoft.com/en-us/library/hh194922.aspx

http://msdn.microsoft.com/en-us/library/hh194922.aspx

Why you'd want to return a Task is a bit of a mystery though. Conceptually, a Task represents a promise that something will happen at some time in the future. In your case, it's already happened, so using a Task is completely pointless.

不过,为什么要返回 Task 有点神秘。从概念上讲,任务代表了在未来某个时间会发生某事的承诺。在你的情况下,它已经发生了,所以使用 Task 是完全没有意义的。

回答by Lasse V. Karlsen

There is nothing in this method indicating that it is an asynchronous method, except for the fact that you've declared it to return Task<int>.

此方法中没有任何内容表明它是一个异步方法,除了您已将其声明为 return Task<int>

However, you're not returning a Task<int>, you're returning count, an int.

但是,您没有返回 a Task<int>,而是返回countint

Since you're not waiting for the action to complete, I would remove the Task<int>return type and replace it with just intinstead as this method is completely synchronous (except for the part you're not waiting for anyway).

由于您不是在等待操作完成,因此我将删除Task<int>返回类型并将其替换为 justint因为此方法是完全同步的(除了您不等待的部分)。

回答by jet_choong

The code here is obviously incorrect. Try to look at the return type in your code. You are returning and intwhich mismatch the return type that expecting a Task<int>. If you are not going to use async await in this method, you can just change your return type to int.

这里的代码显然不正确。尝试查看代码中的返回类型。您正在返回并且与int期望 a 的返回类型不匹配Task<int>。如果您不打算在此方法中使用 async await,则只需将返回类型更改为int.

However, if you insist on returning Task<int>instead of int, you can use the following for your return statement

但是,如果您坚持使用 returnTask<int>而不是int,则可以在 return 语句中使用以下内容

return Task.FromResult(count)

This will wrap your intinto Task<int>. For more information of Task.FromResult, you can visit : https://msdn.microsoft.com/en-us/library/hh194922(v=vs.110).aspxWhat is the use for Task.FromResult<TResult> in C#

这将包装你int进入Task<int>。有关 的更多信息Task.FromResult,您可以访问:https: //msdn.microsoft.com/en-us/library/hh194922(v=vs.110).aspx Task.FromResult<TResult> 在 C# 中有什么用