This works:
EndPoint endPoint = new IPEndPoint(_address, _port);
_socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint);
But this does not:
IPEndPoint endPoint = new IPEndPoint(_address, _port);
_socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint);
(Note the type of endPoint)
Which seems odd. Why does the ref keyword break parameter contravariance?
Because in the method signature, the endPoint
parameter is declared as EndPoint
, not IPEndPoint
; there is no guarantee that the method won't set endPoint
to another kind of EndPoint
, which would not be assignable to a IPEndPoint
variable.
For instance, assume you have a FooEndPoint
class that inherits from EndPoint
, and a Foo
method that takes a ref EndPoint
parameter :
public class FooEndPoint : EndPoint
{
...
}
public void Foo(ref EndPoint endPoint)
{
...
endPoint = new FooEndPoint();
...
}
If you were able to pass a IPEndPoint
to that method, the assigment of a FooEndPoint
to the endPoint
parameter would fail at runtime, because a FooEndPoint
is not a IPEndPoint
Because the method ReceiveFrom can create a new EndPoint - but not IPEndPoint. This parameter works kind of in two ways, so the type needs to match exactly.
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