我想写一个带out参数的async方法,像这样:
public async void Method1()
{
int op;
int result = await GetDataTaskAsync(out op);
}
我如何做到这一点在GetDataTaskAsync?
我想写一个带out参数的async方法,像这样:
public async void Method1()
{
int op;
int result = await GetDataTaskAsync(out op);
}
我如何做到这一点在GetDataTaskAsync?
当前回答
我喜欢Try模式。这是一个整齐的图案。
if (double.TryParse(name, out var result))
{
// handle success
}
else
{
// handle error
}
但是,使用异步是有挑战性的。这并不意味着我们没有真正的选择。下面是Try模式的准版本中可以考虑的异步方法的三种核心方法。
方法1 -输出一个结构
这看起来很像一个sync Try方法,只返回一个元组,而不是一个带out形参的bool类型,我们都知道这在c#中是不允许的。
var result = await DoAsync(name);
if (result.Success)
{
// handle success
}
else
{
// handle error
}
使用一个返回true或false且从不抛出异常的方法。
请记住,在Try方法中抛出异常会破坏该模式的整个目的。
async Task<(bool Success, StorageFile File, Exception exception)> DoAsync(string fileName)
{
try
{
var folder = ApplicationData.Current.LocalCacheFolder;
return (true, await folder.GetFileAsync(fileName), null);
}
catch (Exception exception)
{
return (false, null, exception);
}
}
方法2 -传入回调方法
我们可以使用匿名方法来设置外部变量。它的语法很聪明,尽管有点复杂。小剂量的话,没问题。
var file = default(StorageFile);
var exception = default(Exception);
if (await DoAsync(name, x => file = x, x => exception = x))
{
// handle success
}
else
{
// handle failure
}
该方法遵循Try模式的基本原则,但设置了要传递给回调方法的参数。是这样做的。
async Task<bool> DoAsync(string fileName, Action<StorageFile> file, Action<Exception> error)
{
try
{
var folder = ApplicationData.Current.LocalCacheFolder;
file?.Invoke(await folder.GetFileAsync(fileName));
return true;
}
catch (Exception exception)
{
error?.Invoke(exception);
return false;
}
}
我对这里的表现有一个疑问。但是,c#编译器非常聪明,我认为你选择这个选项是安全的,几乎可以肯定。
方法3 -使用ContinueWith
如果您只是按照设计使用TPL呢?没有元组。这里的思想是使用异常将ContinueWith重定向到两条不同的路径。
await DoAsync(name).ContinueWith(task =>
{
if (task.Exception != null)
{
// handle fail
}
if (task.Result is StorageFile sf)
{
// handle success
}
});
使用在出现任何类型的失败时都会抛出异常的方法。这与返回布尔值不同。这是与TPL沟通的一种方式。
async Task<StorageFile> DoAsync(string fileName)
{
var folder = ApplicationData.Current.LocalCacheFolder;
return await folder.GetFileAsync(fileName);
}
在上面的代码中,如果没有找到文件,则抛出异常。这将调用处理Task的失败ContinueWith。其逻辑块中的异常。整洁的,是吧?
听着,我们喜欢尝试模式是有原因的。从根本上说,它是如此的整洁和可读,因此,是可维护的。当你选择你的方法,看门狗的可读性。记住下一个开发者,他们在6个月后不需要你回答澄清问题。您的代码可能是开发人员拥有的唯一文档。
祝你好运。
其他回答
模式匹配来拯救!c# 9(我认为)之后:
// example of a method that would traditionally would use an out parameter
public async Task<(bool success, int? value)> TryGetAsync()
{
int? value = // get it from somewhere
return (value.HasValue, value);
}
像这样使用它:
if (await TryGetAsync() is (true, int value))
{
Console.WriteLine($"This is the value: {value}");
}
Alex在可读性上有很好的观点。同样,函数也具有足够的接口来定义返回的类型,并且还可以获得有意义的变量名。
delegate void OpDelegate(int op);
Task<bool> GetDataTaskAsync(OpDelegate callback)
{
bool canGetData = true;
if (canGetData) callback(5);
return Task.FromResult(canGetData);
}
调用者提供一个lambda(或一个命名函数),智能感知通过从委托复制变量名来提供帮助。
int myOp;
bool result = await GetDataTaskAsync(op => myOp = op);
这种特殊的方法类似于“Try”方法,如果方法结果为真,则设置myOp。否则,你不会关心myOp。
对于真正想要保持参数的开发人员,这里可能有另一种解决方法。
将参数更改为数组或List以封装实际值。记得在发送到方法之前初始化列表。返回后,在使用它之前一定要检查值是否存在。小心编码。
您可以通过使用TPL(任务并行库)而不是直接使用await关键字来做到这一点。
private bool CheckInCategory(int? id, out Category category)
{
if (id == null || id == 0)
category = null;
else
category = Task.Run(async () => await _context.Categories.FindAsync(id ?? 0)).Result;
return category != null;
}
if(!CheckInCategory(int? id, out var category)) return error
在异步方法中不能有ref或out参数(如前所述)。
这需要在数据移动中进行一些建模:
public class Data
{
public int Op {get; set;}
public int Result {get; set;}
}
public async void Method1()
{
Data data = await GetDataTaskAsync();
// use data.Op and data.Result from here on
}
public async Task<Data> GetDataTaskAsync()
{
var returnValue = new Data();
// Fill up returnValue
return returnValue;
}
您获得了更容易重用代码的能力,而且它比变量或元组更具可读性。