Using serialization is a good way, however it adds a lot of overhead that you probably don't need. I would add two functions, like:
public byte[] ToBytes()
{
using (MemoryStream ms = new MemoryStream)
{
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(clientId);
bw.Write(clientName);
bw.Write(message);
return ms.ToArray()
}
}
public static ClientData FromBytes(byte[] buffer)
{
ClientData retVal = new ClientData();
using (MemoryStream ms = new MemoryStream(buffer))
{
BinaryReader br = new BinaryReader(ms);
retVal.clientId = br.ReadInt32()
retVal.clientName = br.ReadString();
retVal.message = br.ReadString();
}
return retVal;
}
Just make sure to initialize your strings to
string.empty
instead of null so that it can read them and write them correctly.