I'm using JetBrains Rider for programming C#, and I guess this warning will also appear when using ReSharper:
I wrote this function, GetTicket
:
public async Task<IEnumerable<Ticket>> GetTicket(int id)
{
return await _memoryCache.GetOrCreateAsync(_cachingFunctionalty.BuildCachingName(id), entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(10);
return GetTicket_uncached(id);
});
}
and GetTicket_uncached
, which it calls:
private async Task<IEnumerable<Ticket>> GetTicket_uncached(int id)
{
RestClient client = new RestClient(ServiceAdress);
Request req = new Request
{
Method = Method.GET,
Resource = "api/tickets/get",
Parameters = new {ident = id}
};
return await client.ExecuteRequestAsync<Ticket[]>(req);
}
So, the parameter id
in method public async Task<IEnumerable<Ticket>> GetTicket(int id)
is highlighted with following warning:
Closure allocation: 'id' parameter and 'this' reference
I found a few things while Googling but I still don't know what this means and what's the problem?
var localId = id
and uselocalid
instead ofid
, what is simply means is using variable which can be modified outside the scope of current closure and thus can lead to unpredictable results, especially when you work with multi threading – Monarchalid
is a primitive being passed by value, so the analysis is pretty simple. In fact, I'd argue that it's simple enough that Resharper ought to do it. Consistent, sometimes spurious warnings will lead towards consistent, sometimes spurious code...which may be a tradeoff Jetbrains made intentionally. – Benzidine