internal static void Clear(Collection collection)
 {
     if (collection.GetSync(() =>
     {
         if (collection._firstSet == null)
         {
             return(false);
         }
         do
         {
             collection._firstSet.InvokeSync((ValueSet vs) =>
             {
                 if (vs._previous != null || collection._firstSet == null || !ReferenceEquals(collection._firstSet, vs))
                 {
                     throw new InvalidOperationException("Collection was modified");
                 }
                 for (UrlQueryParameter qp = vs._firstParameter; qp != null; qp = vs._firstParameter)
                 {
                     qp.InvokeSync(() =>
                     {
                         if (qp._previousValue != null || qp._set == null || !ReferenceEquals(qp._set, vs))
                         {
                             throw new InvalidOperationException("Collection was modified");
                         }
                         if ((vs._firstParameter = qp._nextValue) != null)
                         {
                             qp._nextValue = qp._nextValue._previousValue = null;
                         }
                         if (qp._key == null)
                         {
                             qp._key = vs._key;
                         }
                         qp._set = null;
                         if (qp._previousParameter == null)
                         {
                             if ((collection._firstParameter = qp._nextParameter) == null)
                             {
                                 collection._lastParameter = null;
                             }
                             else
                             {
                                 qp._nextParameter = qp._nextParameter._previousParameter = null;
                             }
                         }
                         else
                         {
                             if ((qp._previousParameter._nextParameter = qp._nextParameter) == null)
                             {
                                 collection._lastParameter = qp._previousParameter;
                             }
                             else
                             {
                                 qp._nextParameter._previousParameter = qp._previousParameter;
                                 qp._nextParameter = null;
                             }
                             qp._previousParameter = null;
                         }
                     });
                 }
                 if ((collection._firstSet = vs._next) == null)
                 {
                     collection._lastSet = null;
                 }
                 else
                 {
                     vs._next = collection._lastSet._previous = null;
                 }
                 vs._collection = null;
             }, collection._firstSet);
         } while (collection._firstSet != null);
         return(true);
     }))
     {
         collection.OnChange();
     }
 }
                internal static bool Remove(ValueSet vs)
                {
                    if (vs == null)
                    {
                        return(false);
                    }
                    Collection changedColl = vs.GetSync(() =>
                    {
                        if (vs._collection == null)
                        {
                            return(null);
                        }

                        return(vs._collection.GetSync((Collection c) =>
                        {
                            for (UrlQueryParameter qp = vs._firstParameter; qp != null; qp = vs._firstParameter)
                            {
                                qp.InvokeSync(() =>
                                {
                                    if (qp._previousValue != null || qp._set == null || !ReferenceEquals(qp._set, vs))
                                    {
                                        throw new InvalidOperationException("Collection was modified");
                                    }
                                    if ((vs._firstParameter = qp._nextValue) != null)
                                    {
                                        qp._nextValue = qp._nextValue._previousValue = null;
                                    }
                                    if (qp._key == null)
                                    {
                                        qp._key = vs._key;
                                    }
                                    qp._set = null;
                                    if (qp._previousParameter == null)
                                    {
                                        if ((c._firstParameter = qp._nextParameter) == null)
                                        {
                                            c._lastParameter = null;
                                        }
                                        else
                                        {
                                            qp._nextParameter = qp._nextParameter._previousParameter = null;
                                        }
                                    }
                                    else
                                    {
                                        if ((qp._previousParameter._nextParameter = qp._nextParameter) == null)
                                        {
                                            c._lastParameter = qp._previousParameter;
                                        }
                                        else
                                        {
                                            qp._nextParameter._previousParameter = qp._previousParameter;
                                            qp._nextParameter = null;
                                        }
                                        qp._previousParameter = null;
                                    }
                                });
                            }
                            if (vs._previous == null)
                            {
                                if ((c._firstSet = vs._next) == null)
                                {
                                    c._lastSet = null;
                                }
                                else
                                {
                                    vs._next = vs._next._previous = null;
                                }
                            }
                            else
                            {
                                if ((vs._previous._next = vs._next) == null)
                                {
                                    c._lastSet = vs._previous;
                                }
                                else
                                {
                                    vs._next._previous = vs._previous;
                                    vs._next = null;
                                }
                                vs._previous = null;
                            }
                            vs._collection = null;
                            return c;
                        }, vs._collection));
                    });

                    if (changedColl == null)
                    {
                        return(false);
                    }
                    changedColl.OnChange();
                    return(true);
                }