Override Dictionary.Add

前端 未结 5 1907
星月不相逢
星月不相逢 2020-12-17 10:07

I need to know how to override the Add-method of a certain Dictionary in a certain static class. Any suggestions?

If it matters, the dictionary looks like this:

5条回答
  •  春和景丽
    2020-12-17 10:50

    In some scenarios, where TKey is a property of TValue, you may want to consider using KeyedCollection, which allows you to override InsertItem, SetItem, RemoveItem, and ClearItems.
    It uses a dictionary internally (unless specified otherwise via the threshold constructor parameter), but also manages a collection.

    I Haven't benchmarked or compared performance, there might be a performance difference so use carefully.

    Consider voting on this feature suggestion on the .NET Core repo.

提交回复
热议问题