Override Dictionary.Add

前端 未结 5 1893
星月不相逢
星月不相逢 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:45

    It's actually simpler to use the "new" keyword. I do this often when my objects contain thier own unique ID value.

    // Warning Air Code - Subject to bonehead mistakes
    
    internal sealed class SomeDict : Dictionary
    {
        public new void Add(myKeyType key, myObjectType value)
        {
            this.Add(value);
        }
    
        internal void Add(myObjectType value)
        {
            base.Add(value.Id, value);
        }
    
    }
    

提交回复
热议问题