C# 尝试向集合中添加重复项时应该抛出什么异常类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1280035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:37:29  来源:igfitidea点击:

What exception type should be thrown when trying to add duplicate items to a collection?

c#.netexceptionduplicates

提问by klashar

Following code should throw exception to prevent adding duplicate collection item.

以下代码应抛出异常以防止添加重复的集合项。

ICollection<T> collection = new List<T>();

public void Add(T item)
{
    if (collection.Contain(item))
    {
          throw new SomeExceptionType()
    }

    collection.Add(item);
}

What standard exception type is the most appropriate?

什么标准异常类型最合适?

采纳答案by Pavel Minaev

Well, Dictionary<,>.Add()throws ArgumentExceptionif such key already exists, so I guess this could be a precedent.

好吧,如果这样的键已经存在,则Dictionary<,>.Add()抛出ArgumentException,所以我想这可能是一个先例。

回答by Andrew Hare

I would use InvalidOperationException:

我会用InvalidOperationException

The exception that is thrown when a method call is invalid for the object's current state.

当方法调用对于对象的当前状态无效时抛出的异常。

Since the validity of the argument's value is contingent upon the state of the object (that is whether or not collection.Contains(item)is true) I think this is the best exception to use.

由于参数值的有效性取决于对象的状态(即是否collection.Contains(item)为真),我认为这是使用的最佳例外。

Make sure that you add a good message to the exception that makes it clear what the problem was to the caller.

确保为异常添加了一条好的消息,让调用者清楚问题出在哪里。

回答by Brandon

ArgumentExceptionwould probably be the best. This is the exception thrown when an argument is invalid.

ArgumentException可能是最好的。这是参数无效时抛出的异常。

回答by cyberconte

ArgumentException would be the proper exception (Dictionary uses that exception as well)

ArgumentException 将是正确的异常(字典也使用该异常)

回答by Ben M

System.ArgumentException

回答by Thomas Levesque

I'd say InvalidOperationException, because it is not valid to add an object that's already in the collection

我会说InvalidOperationException,因为添加一个已经在集合中的对象是无效的

回答by Paul Williams

I would throw an ArgumentException. That's what the generic System.Collections.Generic.SortedList<>does in its Addmethod.

我会抛出一个 ArgumentException。这就是泛型System.Collections.Generic.SortedList<>在其Add方法中所做的。

From the .NET Framework 2.0 code:

从 .NET Framework 2.0 代码:

    public void Add(TKey key, TValue value)
    {
        if (key == null)
        {
            System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.key);
        }
        int num = Array.BinarySearch<TKey>(this.keys, 0, this._size, key, this.comparer);
        if (num >= 0)
        {
            System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_AddingDuplicate);
        }
        this.Insert(~num, key, value);
    }

回答by amarnath chatterjee

Linq uses two more exceptions DuplicateNameExceptionand DuplicateKeyExceptionyou can use these if you are using system.data assembly.

Linq 使用另外两个异常DuplicateNameExceptionDuplicateKeyException如果您使用 system.data 程序集,您可以使用它们。