I'm waffling between which is better (in terms of aesthetics, idiomatic-ness, and performance):
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);
}
or
public async Task<string> DecryptAsync(string encrypted)
{
SymmetricAlgorithm aes = this.GetAes();
return await this.DecryptAsync(aes, encrypted).ContinueWith(decryptTask =>
{
aes.Dispose();
return decryptTask.Result;
});
}
The main difference being that the second one captures the aes
variable in the lambda while the first one passes it as a parameter then casts it to the appropriate type.
A third consideration, inspired by Oxald and Servy:
public async Task<string> DecryptAsync(string encrypted)
{
using (SymmetricAlgorithm aes = this.GetAes())
{
return await this.DecryptAsync(aes, encrypted);
}
}