So recently we've installed the 'Clr Heap Allocation Analyzer' extension for Visual Studio to check our code for heap allocation problems caused by boxing, among others. We've come across the warning 'HAA0102', which gives the following description:
Non-overridden virtual method call on a value type adds a boxing or constrained instruction
It refers to the condition checks in following line of code:
if(instanceType == Enums.Tags.DialoguePanel.ToString())
{
// Some code handling
}
else if(instanceType == Enums.Tags.InfoPanel.ToString())
{
// Some other code handling
}
For context, the Enums.Tags.DialoguePanel refers to the following enum declaration:
public enum Tags
{
InfoPanel,
DialoguePanel,
WarningPanel
}
Now I understand that enum is a value type and therefore Enums.Tags.DialoguePanel is a value type. I also understand that to .ToString() is basically boxing that to a reference type (string).
My question is whether or not it is necessary to refactor these kinds of lines to avoid boxing (and if so... how?) or if it's better to suppress this warning for these specific cases? Please keep in mind that that we do need to keep the enum types. They're used as a controlled way of providing the user options (in a dropdown list). Refactoring those to be of another type will take a LOT of work (which is fine if the performance will be increased significantly).
Thanks in advance!