ToolboxItemContainer.Equals incorrectly returns false on first invocation
- Dominant language
- C#
- Stars
- 4.9k
- Forks
- 1.1k
- Avg merge
- 1d 13m
- Merged PRs (30d)
- 85
Description
* .NET Core Version:
The latest .Net 6.0 SDK build
* Have you experienced this same bug with .NET Framework?:
Yes, currently released .NET Framework 4.8 (and earlier)
**Problem description:**
ToolboxItemContainer.Equals incorrectly returns false on first invocation.
Visual Studio 2019 is affected by this issue and we currently have a workaround: [Pull request 312663](https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/VS/pullrequest/312663).
**Root cause analysis:**
This is a bug in the code of [System.Drawing.Design.ToolboxItemContainer.Equals](http://ddindex/?rightProject=System.Drawing.Design&file=System%5CDrawing%5CDesign%5CToolboxService.cs&line=1806), as a last resort, invokes GetToolboxItem(null) on both objects and is expected to compare the returned objects. The code (as a PERF optimization) skips this last comparison if the returned objects are the same as the _toolboxItem fields. This overlooks that [GetToolboxItem(null)](http://ddindex/?rightProject=System.Drawing.Design&file=System%5CDrawing%5CDesign%5CToolboxService.cs&line=1976) , when it is invoked on an object where _toolboxItem is null, changes the value of the _toolboxItem field to the object it returns. This prevents the last comparison to ever being executed.
**Steps to reproduce:**
Run the following code
```cs
using System;
using System.Drawing.Design;
namespace ReproToolboxItemContainer
{
class Program
{
static void Main(string[] args)
{
var tbic1a = new ToolboxItemContainer(new ToolboxItem(typeof(string)));
var tbic2a = new ToolboxItemContainer(new ToolboxItem(typeof(string)));
var tbic1b = new ToolboxItemContainer(tbic1a.ToolboxData);
var tbic2b = new ToolboxItemContainer(tbic2a.ToolboxData);
Console.WriteLine(tbic1b.Equals(tbic2b)); // Prints 'False'
Console.WriteLine(tbic1b.Equals(tbic2b)); // Prints 'True'
}
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.