c# - 如何在 C# 中使用 Task<bool> 函数输出字符串值

标签 c# task out

<分区>

如何在 C# 中使用 Task 函数输出字符串值 我需要修复此代码以返回没有字符串值的任务

public Task<bool> DelUserTemp(string UserID, int FingerIndex ,out string result)
{
    return Task.Run(() =>
    {
        if (true)
        {
            result = "done";
            return true;
        }
        else
        {
            result = "error";
            return false;
        }
    });
}

最佳答案

使用 ref/out 的替代方法是返回 C# 7.0 tuples相反。

public Task<(bool Worked, string Result)> DelUserTemp(string UserID, int FingerIndex)
{
    return Task.Run(() =>
    {
        if (true)
        {
            return (true, "done");
        }
        else
        {
            return (false, "error");
        }
    });
}

关于c# - 如何在 C# 中使用 Task<bool> 函数输出字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54685074/

相关文章:

c# - 使用MVVM时,XamlWriter可以读取文本框内容吗?

c# - 对于自动 C# 代码生成 : ApexSql Code or Entity Framework?

c# - Task.Delay 与 DispatcherTimer?

forms - 运行 Gulp 任务 web

c# - 可空列表 <> 作为输出参数

c# - 什么时候应该使用out参数?

c# - 基于其他整数列表控制 LINQ 查询顺序

c# - Windows 8 Metro 应用程序中取消转义 HTML 字符串

c# - 为什么这个任务挂起?

C# 可选输出参数