Boxing / un-boxing

Hi R# developers.

I came across this C# boxing/unboxing article: https://www.reflectionit.nl/blog/2020/csharp-boxing-with-string-concatenation-and-interpolation

and installed the Clr Heap Allocation Analyzer (https://github.com/Microsoft/RoslynClrHeapAllocationAnalyzer) to see how my code would light up.

I noticed a lot of boxing warnings about string interpolation usage like this:

var id = 1;
var message = $"id is {id}, {DateTime.Now:dd-MMM-yyyy HH:mm}";

If I change the code to the following the boxing warnings disappear:

var id = 1;
var message = $"id is {id.ToString()}, {DateTime.Now.ToString("dd-MMM-yyyy HH:mm}")";

However, now R# suggest reversing {DateTime.Now.ToString("dd-MMM-yyyy HH:mm} to {DateTime.Now:dd-MMM-yyyy HH:mm}

I am not experienced enough to decide which is preferable (which I guess may depend on other factors).

Why does R# not warn about boxing?

Thanks for you time.

 

0
3 comments
Official comment

Hi mma71,

 

ReSharper doesn't support Clr Heap Allocation Analyzer, it's VS that highlights these errors. You can easily check that by disabling ReSharper in VS Tools | Options | ReSharper Ultimate page.

I'd suggest contacting package developers for more information about package inspections.

Thank you.

Hi Angeline,

thanks for your reply.

I know about "ReSharper not supporting Clr Heap Allocation Analyzer", however, I'm wondering why does R# not warn about boxing?

 

0

ReSharper has its own "heap allocations viewer" plugin which might help:

https://plugins.jetbrains.com/plugin/9223-heap-allocations-viewer

0

Please sign in to leave a comment.