C#8理解使用语法

我有下一个方法:

public async Task<IEnumerable<Quote>> GetQuotesAsync()
{
    using var connection = new SqlConnection(_connectionString);

    var allQuotes = await connection.QueryAsync<Quote>(@"SELECT [Symbol],[Bid],[Ask],[Digits] FROM [QuoteEngine].[RealtimeData]");

    return allQuotes;
}

一切都很好,连接会放在示波器的末尾。

但是resharper建议将其更改为:

public async Task<IEnumerable<Quote>> GetQuotesAsync()
{
    await using var connection = new SqlConnection(_connectionString);

    var allQuotes = await connection.QueryAsync<Quote>(@"SELECT [Symbol],[Digits] FROM [QuoteEngine].[RealtimeData]");

    return allQuotes;
}

它在使用前添加了一个等待,并且代码已成功编译。这是什么意思,什么时候需要做?

a617027242 回答:C#8理解使用语法

类似于#Make the bastion host,and add it to the just_created group - hosts: 127.0.0.1 roles: - terraform_logic_add_host_logic - hosts: just_created #aka bastion tasks: - name: Include task list in play include: "get_the_private_ip_and_add_to_behind_bastion_group.yml" # Login into behind_bastion group..... - hosts: behind_bastion_group vars: - ansible_connection: ssh - ansible_ssh_common_args: '-o ProxyCommand="ssh -i {{ some_pem_key }} -o StrictHostKeyChecking=no -W %h:%p -q ec2-user@{{ the_bastion_ip }}"' tasks: - name: Include task list in play include: "do_stuff_finally.yml" 使用using (...)清理资源,IDispose使用IAsyncDisposable。 这样一来,清除操作即可执行耗时的任务(例如,涉及I / O)而不会阻塞。

,

如果await using (...)实现了SqlConnection接口,Resharper建议您切换到IAsyncDisposable以使用await using方法异步处理它

DisposeAsync
本文链接:https://www.f2er.com/3127956.html

大家都在问