Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify a variable implementing 2 interfaces in .net

Tags:

.net

vb.net

clr

Is is possible to implement a type specifier with 2 interfaces in .net? Something like:

Public Sub New(obj as ICollection and INotifyCollectionChanged)
    ''Initialize Code
End Sub
like image 802
PeterM Avatar asked Dec 05 '25 13:12

PeterM


2 Answers

No, you will have to define an interface that inherits from both of your desired interfaces.

Public Interface IMyCombinedInterface
    Inherits IColllection, INotifyCollectionChanged

End Interface
like image 130
John Saunders Avatar answered Dec 09 '25 00:12

John Saunders


You can with generic constraints; for example in c#:

public void Something<T>(T obj)
    where T : IFoo, IBar
{....}

Then Something(value) will work only when value is typed as something that implements both IFoo and IBar.

like image 43
Marc Gravell Avatar answered Dec 09 '25 00:12

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!