我在谈论哪一种更好(在美学、习惯性和性能方面):
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);
}
}发布于 2018-03-26 13:40:05
考虑使用“等待”代替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;
};
}https://stackoverflow.com/questions/49409775
复制相似问题