Closing appropriations in C #

I installed the Clar Heap Allocation Analyzer extension, and in the project I see something that I absolutely do not understand, I have a method with a signature

public Task<int> ExecuteAsync(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
    {
        param = SetModificationValuesForGlobalRing(param);
        return _sqlPolicy.ExecuteAsync(async () =>
        {
            int result;
            using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync();
                result = await connection.ExecuteAsync(sql, param as object, transaction, commandTimeout, commandType);
            }
            return result;
        });
    }

These tools give me a warning about the method and all the parameters that say

The compiler will produce a class that will contain this as a field to allow capture of this closure.

I do not know why this is happening, due to optional parameters?

-------- EDIT Add body to the method ------------

+4
source share
1 answer

You must be:

  • Call this code from an anonymous function, such as lambda.

    • or -
  • Using the yield / await keyword.

, # , , ( ) / ( ).

# , , - . , . , /.

, yield/wait . , , ( , , /, ).

, , - , ., , .

+5

All Articles