Search code examples
c#removeall

C# RemoveAll return value and count objects removed


I am new to C# and I have a method that uses removeAll to remove objects from a list but I am not quite sure what the return value is. I have looked around but am struggling to find a definitive answer.

does the method return a value of 1 or 0 based on if an object was removed or does it return the number of objects removed? if it is just returning 1 or 0 how would i go about counting the number of objects that have been removed?

    public bool Remove(string name)
    {
        if (this.list.RemoveAll(x => x.Name.Equals(name)) == 1)
        {
            return true;
        }

        return false;
    } 

Solution

  • According to MSDN for List.RemoveAll()

    Return Value Type: System.Int32 The number of elements removed from the List.

    So you just can return this.list.RemoveAll(x => x.Name.Equals(name))