I have a question regarding Async methods, return and using statements I could not find an answer to.
Having the following Two blocks of Code. Would both behave the same way? Is there any risk in using Option 1?
Option 1:
public async Task<IEnumerable<Devices>> GetPreviousDevicesAsync(string token)
{
IEnumerable<Devices> devices;
using (var connection = _dbConnectionFactory.CreateConnection())
{
devices = await connection.FindAsync<Devices>(statement => statement
.Where($"{nameof(Devices.Token)} = '{token}');
}
return devices;
}
Option 2:
public async Task<IEnumerable<Devices>> GetPreviousDevicesAsync(string token)
{
IEnumerable<Devices> devices;
using (var connection = _dbConnectionFactory.CreateConnection())
{
devices = await connection.FindAsync<Devices>(statement => statement
.Where($"{nameof(Devices.Token)} = '{token}');
return devices;
}
}
I Was wondering if somehow Option 1 code block would keep the connection open as the task would complete after the using block has been left.
Hope you can help with that.
Thanks!