footballism wrote:
W3bbo wrote:
So... uhm... why is Application["Processors"] being treated like a value-type?

Yep, your assumption to some extent is right, but actually HttpApplicationState is not a value type, it's still a reference type, because HttpApplicationState inherits NameObjectCollectionBase which is a Hashtable based collection. when you retrieve an entry from HttpApplicationState collection, for instance:
ProcessorCollection pc = (ProcessorCollection) Application["Processors"];
the HttpApplicationState will internally end up calling Hashtable's get_Item(Object key) method. in get_Item method,the hash code for the key object will be abtained first,this hash code identifies the Hashtable.bucket that is now searched looking for a stored key object that matches the specified key object, when a match is found,it will return the value corresponding to the specified key object, the pseudo code for this operation is something like the  following:
public virtual Object  get_Item(Object key)
{
    Hashtable.bucket[] buckets = this.buckets;
    foreach(Hashtable.bucket b in buckets)
    {
       if(this.KeyEquals(b.key, key))
       {
          return b.Val;
       }
    }

    return null;
}
the definition for Hashtable.bucket type is something like the following:
private struct bucket
{
    public Object key;
    public Object val;
}

because Hashtable.bucket is a struct, which is a value type, so every time you retrieve an object from Hashtable based collection, you actually end up having a private copy of that object.

I think why Microsoft implements the Hashtable collection the way it is now is basically for thread-safe purpose, so every time you want to set new value to the HttpApplicationState collection entry, you have to lock it first, for instance:
lock(Application.GetType())
{
    Application["Processors"] = pc;
}

Sheva


Thanks for clearing that one up footballism Smiley