ConcurrentDictionary<TKey,TValue>.TryAdd(TKey, TValue) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
ConcurrentDictionary<TKey,TValue> に対して、指定したキーと値の追加を試みます。
public:
bool TryAdd(TKey key, TValue value);
public bool TryAdd (TKey key, TValue value);
member this.TryAdd : 'Key * 'Value -> bool
Public Function TryAdd (key As TKey, value As TValue) As Boolean
パラメーター
- key
- TKey
追加する要素のキー。
- value
- TValue
追加する要素の値。 参照型の場合は null
の値を使用できます。
戻り値
キーと値のペアが true
に正常に追加された場合は ConcurrentDictionary<TKey,TValue>。キーが既に存在する場合は false
。
例外
key
が null
です。
ディレクトリに含まれている要素が多すぎます。
例
次の例は、 メソッドを呼び出す方法を 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!");
}
}
// Demonstrates:
// ConcurrentDictionary<TKey, TValue>.TryAdd()
// ConcurrentDictionary<TKey, TValue>.TryUpdate()
// ConcurrentDictionary<TKey, TValue>.TryRemove()
let mutable numFailures = 0 // for bookkeeping
// Construct an empty dictionary
let cd = ConcurrentDictionary<int, string>()
// This should work
if cd.TryAdd(1, "one") |> not then
printfn "CD.TryAdd() failed when it should have succeeded"
numFailures <- numFailures + 1
// This shouldn't work -- key 1 is already in use
if cd.TryAdd(1, "uno") then
printfn "CD.TryAdd() succeeded when it should have failed"
numFailures <- numFailures + 1
// Now change the value for key 1 from "one" to "uno" -- should work
if cd.TryUpdate(1, "uno", "one") |> not then
printfn "CD.TryUpdate() failed when it should have succeeded"
numFailures <- numFailures + 1
// 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") then
printfn "CD.TryUpdate() succeeded when it should have failed"
numFailures <- numFailures + 1
// Remove key/value for key 1. Should work.
let mutable value1 = ""
if cd.TryRemove(1, &value1) |> not then
printfn "CD.TryRemove() failed when it should have succeeded"
numFailures <- numFailures + 1
// Remove key/value for key 1. Shouldn't work, because I already removed it
let mutable value2 = ""
if cd.TryRemove(1, &value2) then
printfn "CD.TryRemove() succeeded when it should have failed"
numFailures <- numFailures + 1
// If nothing went wrong, say so
if numFailures = 0 then
printfn " OK!"
'Imports System.Collections.Concurrent
Class CD_TryXYZ
' Demonstrates:
' ConcurrentDictionary<TKey, TValue>.TryAdd()
' ConcurrentDictionary<TKey, TValue>.TryUpdate()
' ConcurrentDictionary<TKey, TValue>.TryRemove()
Shared Sub Main()
Dim numFailures As Integer = 0
' for bookkeeping
' Construct an empty dictionary
Dim cd As ConcurrentDictionary(Of Integer, [String]) = New ConcurrentDictionary(Of Integer, String)()
' This should work
If Not cd.TryAdd(1, "one") Then
Console.WriteLine("CD.TryAdd() failed when it should have succeeded")
numFailures += 1
End If
' This shouldn't work -- key 1 is already in use
If cd.TryAdd(1, "uno") Then
Console.WriteLine("CD.TryAdd() succeeded when it should have failed")
numFailures += 1
End If
' Now change the value for key 1 from "one" to "uno" -- should work
If Not cd.TryUpdate(1, "uno", "one") Then
Console.WriteLine("CD.TryUpdate() failed when it should have succeeded")
numFailures += 1
End If
' 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") Then
Console.WriteLine("CD.TryUpdate() succeeded when it should have failed")
numFailures += 1
End If
' Remove key/value for key 1. Should work.
Dim value1 As String = ""
If Not cd.TryRemove(1, value1) Then
Console.WriteLine("CD.TryRemove() failed when it should have succeeded")
numFailures += 1
End If
' Remove key/value for key 1. Shouldn't work, because I already removed it
Dim value2 As String = ""
If cd.TryRemove(1, value2) Then
Console.WriteLine("CD.TryRemove() succeeded when it should have failed")
numFailures += 1
End If
' If nothing went wrong, say so
If numFailures = 0 Then
Console.WriteLine(" OK!")
End If
End Sub
End Class
注釈
このメソッドは、 false
キーが既に存在する場合に を返します。 キーが TryUpdate 既に存在する場合に備えて、 メソッドまたは AddOrUpdate メソッドを使用して値を更新します。
適用対象
こちらもご覧ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET