See attached image.
Note also that in this case the Receive method is provided by the auto-complete function. If it's selected, ideally the correct cast would be inserted at that time.
If you choose any of the provided options, a visibility modifier is prepended to the target member which breaks the code.
Better options would be:
- Use explicit interface method
- Convert method to implicit implementation
So, instead of...
public void Add(T item) { (IReceiver<T>)this).Receive(item) }...we have...
public void Add(T item) { if (_disposed) throw new ObjectDisposedException("CrossThreadBuffer"); lock (_buffer) _buffer.Receive(item); _event.Set(); } void IReceiver<T>.Receive(T item) { Add(item); }This avoids any casting. The runtime might even inline the methods or allocate them both as the same entry in the method table, if the type was sealed (pure speculation).