本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

ConcurrentDictionary<TKey, TValue>.TryAdd 方法 (TKey, TValue)

.NET Framework (current version)
 

发布日期: 2016年7月

尝试将指定的键和值添加到 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中。

命名空间:   System.Collections.Concurrent
程序集:  mscorlib(位于 mscorlib.dll)

public bool TryAdd(
	TKey key,
	TValue value
)

参数

key
Type: TKey

要添加的元素的键。

value
Type: TValue

要添加的元素的值。 对于引用类型,该值可以为 null

返回值

Type: System.Boolean

如果成功地将键/值对添加到 true,则为 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>;如果该键已存在,则为 false

Exception Condition
ArgumentNullException

keynull

OverflowException

字典中已包含元素的最大数量(MaxValue)。

此方法返回false如果该键已存在。 使用TryUpdateAddOrUpdate方法来更新的值,如果密钥已存在。

下面的示例演示如何调用ConcurrentDictionary<TKey, TValue>.TryAdd方法︰

class CD_TryXYZ
{
        // Demonstrates:
        //      ConcurrentDictionary<TKey, TValue>.TryAdd()
        //      ConcurrentDictionary<TKey, TValue>.TryUpdate()
        //      ConcurrentDictionary<TKey, TValue>.TryRemove()
        static void Main()
        {
            int numFailures = 0; // for bookkeeping

            // Construct an empty dictionary
            ConcurrentDictionary<int, String> cd = new ConcurrentDictionary<int, string>();

            // This should work
            if (!cd.TryAdd(1, "one"))
            {
                Console.WriteLine("CD.TryAdd() failed when it should have succeeded");
                numFailures++;
            }

            // This shouldn't work -- key 1 is already in use
            if (cd.TryAdd(1, "uno"))
            {
                Console.WriteLine("CD.TryAdd() succeeded when it should have failed");
                numFailures++;
            }

            // Now change the value for key 1 from "one" to "uno" -- should work
            if (!cd.TryUpdate(1, "uno", "one"))
            {
                Console.WriteLine("CD.TryUpdate() failed when it should have succeeded");
                numFailures++;
            }

            // Try to change the value for key 1 from "eine" to "one" 
            //    -- this shouldn't work, because the current value isn't "eine"
            if (cd.TryUpdate(1, "one", "eine"))
            {
                Console.WriteLine("CD.TryUpdate() succeeded when it should have failed");
                numFailures++;
            }

            // Remove key/value for key 1.  Should work.
            string value1;
            if (!cd.TryRemove(1, out value1))
            {
                Console.WriteLine("CD.TryRemove() failed when it should have succeeded");
                numFailures++;
            }

            // Remove key/value for key 1.  Shouldn't work, because I already removed it
            string value2;
            if (cd.TryRemove(1, out value2))
            {
                Console.WriteLine("CD.TryRemove() succeeded when it should have failed");
                numFailures++;
            }

            // If nothing went wrong, say so
            if (numFailures == 0) Console.WriteLine("  OK!");
        }
}

通用 Windows 平台
自 8 起可用
.NET Framework
自 4.0 起可用
可移植类库
可移植 .NET 平台 中受支持
Windows Phone
自 8.1 起可用
返回页首
显示: