dotnet / dotnet/roslyn

IDE0200 recommends change that increases allocation

Open
#73,147 8 comments 0 reactions 0 assignees View on GitHub
Area-IDE Bug Feature - IDE0200
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**:
Version 17.11.0 Preview 1.0 [34810.211.main]

**Steps to Reproduce**:
```C#
public class C
{
private static readonly Invoker s_invoker = new();

public void M1() => Use(s_invoker.Invoke);
public void M2() => Use(text => s_invoker.Invoke(text));

public void Use(Func func) {}
}

public class Invoker
{
public int Invoke(string text) => 42;
}
```

**Diagnostic Id**:
IDE0200

**Expected Behavior**:
No diagnostic, or at least a different diagnostic code indicating the change will be making the code less efficient.

**Actual Behavior**:
IDE0200 gets raised on M2 and recommends it be changed to the equivalent of M1. But M1 always allocates a new delegate whereas M2 as written above caches the delegate such that ammortized it only ever allocates once.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.