tags:

views:

191

answers:

1

Finding the size of .net hashtable when deserialzing

If I read from a stream and get a hashtable out. Is their a good way to know how large the hashtable would be. Ie, if I look the file in a binary editor how many of the bytes represent this hashtable? How does the Deserialize mehthod know what goes into the hash?

IFormatter formatter = new BinaryFormatter();
table = (Hashtable)formatter.Deserialize(FileStream);

Edit: What I am trying to figure out is if I were to look at the file in a binary editor how much of that data in the filestream is my hashtable. Or can I do something like a sizeof() to find out the size (I suspect I can't).

A: 

Perhaps this snippet of code might help? Shameless borrowed from Victor Garcia Aprea. The article suggests that the size variable will hold the number of bytes. Granted this is serialized to a string, and not binary as you're looking for.

Hashtable ht = new Hashtable();
for (int i = 0; i < 100; i++)
{
    ht.Add(i, (i*10000).ToString());
}

LosFormatter los = new LosFormatter(); //in System.Web namespace; used for ViewState
StringWriter sw = new StringWriter();
los.Serialize(sw, ht);
string resultSt = sw.GetStringBuilder().ToString();

int size = sw.GetStringBuilder().ToString().Length;

Console.WriteLine(size);
p.campbell