Using my code examples why don't the first two objects equal each other when converted to binary?
I know that I could write an override for the equals method and use that to check if two objects are alike. However I was experimenting with this method. Why do two identical objects convert to different binary values?
Given said class:
[Serializable]
public class HashingSample
{
public int Number { get; set; }
public HashingSample(int num)
{
Number = num;
}
public byte[] ToBinary()
{
using (MemoryStream stream = new MemoryStream())
{
var binaryFormatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, this);
return stream.ToArray();
}
}
}
And this code:
var s1 = new HashingSample(1).ToBinary();
var s1_2 = new HashingSample(1).ToBinary();
var s2 = new HashingSample(2).ToBinary();
var first2 = s1.Equals(s1_2);