我在胡扯之间更好(在美学,惯用性和表现方面):
public async Task<string> DecryptAsync(string encrypted)
{
SymmetricAlgorithm aes = this.GetAes();
return await this.DecryptAsync(aes, encrypted).ContinueWith(
(decryptTask, objectState) =>
{
(objectState as IDisposable)?.Dispose();
return decryptTask.Result;
},
aes);
}
或
public async Task<string> DecryptAsync(string encrypted)
{
SymmetricAlgorithm aes = this.GetAes();
return await this.DecryptAsync(aes, encrypted).ContinueWith(decryptTask =>
{
aes.Dispose();
return decryptTask.Result;
});
}
主要区别在于第二个变量捕获了lambda中的aes
变量,而第一个变量将其作为参数传递,然后将其转换为适当的类型。
public async Task<string> DecryptAsync(string encrypted)
{
using (SymmetricAlgorithm aes = this.GetAes())
{
return await this.DecryptAsync(aes, encrypted);
}
}
答案 0 :(得分:0)
考虑使用await而不是ContinueWith。等待的结果等于Task.Result。可以使用using语句来处理aes:
public async Task<string> DecryptAsync(string encrypted)
{
using (SymmetricAlgorithm aes = this.GetAes())
{
string decryptedText = await this.DecryptAsync(aes, encrypted);
return decryptedText;
};
}