我ConcurrentDictionary<TKey, TValue>
用来实现一个ConcurrentSet<T>
.
public class ConcurrentSet<T> : ISet<T>
{
private readonly ConcurrentDictionary<T, byte> collection;
}
ConcurrentDictionary<TKey, TValue>
不能包含具有空键的对。
// summary, param, returns...
/// <exception cref="ArgumentNullException">
/// <paramref name="item" /> is null.
/// </exception>
public bool Add(T item)
{
// This throws an argument null exception if item is null.
return this.collection.TryAdd(item, 0);
}
那么,我应该,
if (item == null)
throw new ArgumentNullException("item", "Item must not be null.");
return this.collection.TryAdd(item, 0);
或者,我应该,
try
{
return this.collection.TryAdd(item, 0);
}
catch (ArgumentNullException)
{
throw new ArgumentNullException("item", "Item must not be null.");
}
或者,我应该,
try
{
return this.collection.TryAdd(item, 0);
}
catch (ArgumentNullException x)
{
// I know, but I don't want to preserve the stack trace
// back to the underlying dictionary, anyway.
throw x;
}
或者,我应该,
try
{
return this.collection.TryAdd(item, 0);
}
catch (ArgumentNullException)
{
// The thrown exception will have "key", instead of
// "item" as the parameter's name, in this instance.
throw;
}
这样做的正确方法是什么?