ReadOnlyCollection <T> implements IList <T>, but does not implement the add method

How does it System.Collections.ObjectModel.ReadOnlyCollection<T>implement System.Collections.Generic.IList<T>but not implement its method Add?

I am not asking why it does not have a method Add- this is obvious because it should be read-only; I ask how this can do without implementing the method that is required by the interface contract IList<T>.

+4
source share
3 answers

He explicitly implements the method along with several others that usually modify the base collection. See the Explicit Interface Implementations section on the next page:

ReadOnlyCollection<T>

Add , :

. , ReadOnlyCollection<T> ICollection<T>.

Add ReadOnlyCollection<T> ( ). , - , ( ).

. ( #)


:

, , - , . , IOleCommandTarget.QueryStatus ( ). . - .

/// <inheritdoc/>
int IOleCommandTarget.QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
    using (OleCommandText oleCommandText = OleCommandText.FromQueryStatus(pCmdText))
    {
        Guid cmdGroup = guidCmdGroup;
        for (uint i = 0; i < cCmds; i++)
        {
            OLECMDF status = QueryCommandStatus(ref cmdGroup, prgCmds[i].cmdID, oleCommandText);
            if (status == default(OLECMDF) && _next != null)
            {
                int hr = _next.QueryStatus(ref cmdGroup, cCmds, prgCmds, pCmdText);
                if (ErrorHandler.Failed(hr))
                    return hr;
            }
            else
            {
                prgCmds[i].cmdf = (uint)status;
            }
        }

        return VSConstants.S_OK;
    }
}

protected virtual public virtual, pCmdText _next prgCmds ( ).

/// <summary>
/// Gets the current status of a particular command.
/// </summary>
/// <remarks>
/// The base implementation returns 0 for all commands, indicating the command is
/// not supported by this command filter.
/// </remarks>
/// <param name="commandGroup">The command group.</param>
/// <param name="commandId">The command ID.</param>
/// <param name="oleCommandText">A wrapper around the <see cref="OLECMDTEXT"/>
/// object passed to <see cref="IOleCommandTarget.QueryStatus"/>, or
/// <see langword="null"/> if this parameter should be ignored.</param>
/// <returns>A collection of <see cref="OLECMDF"/> flags indicating the current
/// status of a particular command, or 0 if the command is not supported by the
/// current command filter.</returns>
protected virtual OLECMDF QueryCommandStatus(ref Guid commandGroup, uint commandId, OleCommandText oleCommandText)
{
    return default(OLECMDF);
}
+4

ReadOnlyCollection<>, , Add :

int IList.Add(object value)
{
    ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
    return -1;
}

IList.Add. , Add ReadOnlyCollection<>. IList, , NotSupportedException.

, IList, Clear, Insert Remove , ICollection<>: .

.NET 4.5, System.Collections.Generic.IReadOnlyCollection<>. ReadOnlyCollection<> , IList, , , . , 4.5 , , IList IReadOnlyCollection<>.

+1

It is implemented in this way:

int IList.Add(object value)
{
    ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
    return -1;
}

It is also documented on MSDN :

Adds an item to an IList. This implementation always throws a NotSupportedException.

+1
source

Source: https://habr.com/ru/post/1532847/


All Articles