Let's say I want to express "a method that expect an object which implements interfaces IFoo and IBar". Something like:
void Method(IFoo+IBar param);
How can I say this using C# ?
(Is there a syntaxical construct? Or a good idiom perhaps?)
Introduce an interface:
interface IFooBar : IFoo, IBar {}
void Method(IFooBar param);
This is bad and I regret even thinking of it :-) Looks OK at the first glance, but the sad part is that silently introduces a dependency which shouldn't be here.
The sole reason for IFooBar
to exist is to be a part of Method
's interface. Hence, any class of objects to be used as Method
's parameter must know that this method (and that interface) exist - which wouldn't be the case otherwise. All classes implementing IFoo and IBar would need to be modified to also implement IFooBar (possibly getting to know a new assembly) - which is heavily impractical, or even impossible if we can't modify them.
Unnecessary dependency = a no-go.
Give up static typing:
void Method(object param)
{
if (!param is IFoo)
throw new ArgumentException("IFoo not supported", "param");
if (!param is IBar)
throw new ArgumentException("IBar not supported", "param");
// ...
}
(or: define one type in signature and dynamically check the others - preferable if one is the most important, but even more confusing)
Works correcty, but in run-time (no compile-time check). Desperately requires a doc (and everyone to read it).
Also only practical in function parameters. The code'd get massively bloated with casts if I tried to use that on a field.
A real case for this situation? For instance IList<Foo>
+ INotifyCollectionChanged
.
public void someMethod<T>(T param) where T : IFoo, IBar
{...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With