Answer the question
In order to leave comments, you need to log in
Why such a big difference in data volume?
Good afternoon.
It took me in a training project to send data from the client to the server and back. I wrote a simple semblance of a "package manager".
public class DataWriter : BinaryWriter
{
private MemoryStream _ms;
private BinaryFormatter _bf;
public DataWriter()
:base()
{
_ms = new MemoryStream();
_bf = new BinaryFormatter();
OutStream = _ms;
}
/// <summary>
/// Method for Objects.
/// </summary>
/// <param name="o"></param>
public void WriteObject(object o)
{
_bf.Serialize(_ms, o);
}
public byte[] GetBytes()
{
Close();
byte[] data = _ms.ToArray();
return data;
}
}
public class DataReader :BinaryReader
{
private BinaryFormatter _bf;
public DataReader(byte [] data)
:base(new MemoryStream(data))
{
_bf = new BinaryFormatter();
}
public T ReadObject<T>()
{
return (T)_bf.Deserialize(BaseStream);
}
}
[Serializable]
public class Artist
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string SongTitle { get; set; }
}
static void Main(string[] args)
{
Console.Title = "Data Into Packets Lib. Tester";
Artist artist = new Artist();
artist.FirstName = "Michael";
artist.LastName = "Jackson";
artist.SongTitle = "Give In To Me";
DataWriter dw = new DataWriter();
dw.WriteObject(artist);
byte[] data = dw.GetBytes();
Console.WriteLine($"Values:\nName: {artist.FirstName}.\nLastName: {artist.LastName}.\nSong Title: {artist.SongTitle}");
Console.WriteLine($"Class object size: {data.Length} bytes");
dw = new DataWriter();
dw.Write(artist.FirstName);
dw.Write(artist.LastName);
dw.Write(artist.SongTitle);
data = dw.GetBytes();
Console.WriteLine($"Simple variable size: {data.Length} bytes");
Console.ReadKey();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question