The codes below are exactly the same, except that one is C# and the other one is VB.Net. C# compiles just fine, but VB.Net throws the warning:
Interface 'System.IObserver(Of Foo)' is ambiguous with another implemented interface 'System.IObserver(Of Bar)' due to the 'In' and 'Out' parameters in 'Interface IObserver(Of In T)'
Why does VB.Net show the warning and not C#? And most important, how can I resolve this problem?
Obs: I'm using .Net Framework 4 with Visual Studio 2010 Ultimate.
VB.Net Code:
Module Module1
Sub Main()
End Sub
Public Class Foo
End Class
Public Class Bar
End Class
Public Class Beholder
Implements IObserver(Of Foo)
Implements IObserver(Of Bar)
#Region "Impl"
Public Sub OnCompleted() Implements System.IObserver(Of Bar).OnCompleted
End Sub
Public Sub OnError([error] As System.Exception) Implements System.IObserver(Of Bar).OnError
End Sub
Public Sub OnNext(value As Bar) Implements System.IObserver(Of Bar).OnNext
End Sub
Public Sub OnCompleted1() Implements System.IObserver(Of Foo).OnCompleted
End Sub
Public Sub OnError1([error] As System.Exception) Implements System.IObserver(Of Foo).OnError
End Sub
Public Sub OnNext1(value As Foo) Implements System.IObserver(Of Foo).OnNext
End Sub
#End Region
End Class
End Module
C# Code:
class Program {
static void Main(string[] args) {
}
}
public class Foo { }
public class Bar { }
public class Beholder : IObserver<Foo>, IObserver<Bar> {
#region IObserver<Foo> Members
public void OnCompleted() {
throw new NotImplementedException();
}
public void OnError(Exception error) {
throw new NotImplementedException();
}
public void OnNext(Foo value) {
throw new NotImplementedException();
}
#endregion
#region IObserver<Bar> Members
public void OnNext(Bar value) {
throw new NotImplementedException();
}
#endregion
}
Foo
/Bar
are independent classes. I can't think of anyT
for whichIObserver<T>
isIObserver<Foo>
andIObserver<Bar>
at the same time. – EsbenshadeIEnumerable<T>
this warning would make sense, sinceIEnumerable<Foo>
andIEnumerable<Bar>
are bothIEnumerable<object>
, exactly becauseIEnumerable<out T>
is covariant. And it would also make sense with the contravariantIObserver<T>
if either of them were an interface, or if one were the base class of the other. – Esbenshade