There is an overload of UdpClient.Send method that accepts dgram, bytes, and an endpoint.
public int Send(byte[] dgram, int bytes, IPEndPoint endPoint)
in MSDN documentation, the description for the ‘bytes’ parameter is as following.
bytes
Type : System.Int32
The number of bytes in the datagram.
Why is there a ‘bytes’ parameter? Isn’t it possible to figure out the number of bytes in the dgram array inside the Send method?
Because when you “Send” on the network you are typically sending a stream of bytes that you have encoded from some other source. Serialisation etc.
You use this overload so that you can provide a buffer and potentially send just a subsection of the bytes of that buffer.
Often you’ll create a larger buffer, but you might only populate a section of it. This way you can just send that buffer and say Please only send X bytes from my buffer.