internal void SetKeyedObject(int key, object value)
 {
     if (key-- == Root)
     {
         if (value == null)
         {
             throw new ArgumentNullException("value");
         }
         if (rootObject != null && ((object)rootObject != (object)value))
         {
             throw new ProtoException("The root object cannot be reassigned");
         }
         rootObject = value;
     }
     else
     {
         IObjectCache cache = GetCacheForWrite();
         if (key < cache.Count)
         {
             cache.SetObjectKey(value, key);
         }
         else
         {
             if (key != cache.Append(value))
             {
                 throw new ProtoException("Internal error; a key mismatch occurred");
             }
         }
     }
 }
        internal int AddObjectKey(object value, out bool existing)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((object)value == (object)rootObject) // (object) here is no-op, but should be
            {                                        // preserved even if this was typed - needs ref-check
                existing = true;
                return(Root);
            }

            string       s     = value as string;
            IObjectCache cache = GetCacheForWrite();

            int key = cache.GetObjectKey(value);

            if (!(existing = key >= 0))
            {
                key = cache.Append(value);
            }
            return(key + 1);
        }