@@ -48,10 +48,7 @@ public ConditionalWeakTable()
48
48
/// If the key is not found, contains default(TValue).
49
49
/// </param>
50
50
/// <returns>Returns "true" if key was found, "false" otherwise.</returns>
51
- /// <remarks>
52
- /// The key may get garbage collected during the TryGetValue operation. If so, TryGetValue
53
- /// may at its discretion, return "false" and set "value" to the default (as if the key was not present.)
54
- /// </remarks>
51
+ /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
55
52
public bool TryGetValue ( TKey key , [ MaybeNullWhen ( false ) ] out TValue value )
56
53
{
57
54
if ( key is null )
@@ -65,12 +62,8 @@ public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
65
62
/// <summary>Adds a key to the table.</summary>
66
63
/// <param name="key">key to add. May not be null.</param>
67
64
/// <param name="value">value to associate with key.</param>
68
- /// <remarks>
69
- /// If the key is already entered into the dictionary, this method throws an exception.
70
- /// The key may get garbage collected during the Add() operation. If so, Add()
71
- /// has the right to consider any prior entries successfully removed and add a new entry without
72
- /// throwing an exception.
73
- /// </remarks>
65
+ /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
66
+ /// <exception cref="ArgumentException"><paramref name="key"/> is already entered into the dictionary.</exception>
74
67
public void Add ( TKey key , TValue value )
75
68
{
76
69
if ( key is null )
@@ -94,6 +87,7 @@ public void Add(TKey key, TValue value)
94
87
/// <param name="key">The key to add.</param>
95
88
/// <param name="value">The key's property value.</param>
96
89
/// <returns>true if the key/value pair was added; false if the table already contained the key.</returns>
90
+ /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
97
91
public bool TryAdd ( TKey key , TValue value )
98
92
{
99
93
if ( key is null )
@@ -117,6 +111,7 @@ public bool TryAdd(TKey key, TValue value)
117
111
/// <summary>Adds the key and value if the key doesn't exist, or updates the existing key's value if it does exist.</summary>
118
112
/// <param name="key">key to add or update. May not be null.</param>
119
113
/// <param name="value">value to associate with key.</param>
114
+ /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
120
115
public void AddOrUpdate ( TKey key , TValue value )
121
116
{
122
117
if ( key is null )
@@ -141,13 +136,9 @@ public void AddOrUpdate(TKey key, TValue value)
141
136
}
142
137
143
138
/// <summary>Removes a key and its value from the table.</summary>
144
- /// <param name="key">key to remove. May not be null.</param>
145
- /// <returns>true if the key is found and removed. Returns false if the key was not in the dictionary.</returns>
146
- /// <remarks>
147
- /// The key may get garbage collected during the Remove() operation. If so,
148
- /// Remove() will not fail or throw, however, the return value can be either true or false
149
- /// depending on who wins the race.
150
- /// </remarks>
139
+ /// <param name="key">The key to remove.</param>
140
+ /// <returns><see langword="true"/> if the key is found and removed; otherwise, <see langword="false"/>.</returns>
141
+ /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
151
142
public bool Remove ( TKey key )
152
143
{
153
144
if ( key is null )
@@ -157,7 +148,25 @@ public bool Remove(TKey key)
157
148
158
149
lock ( _lock )
159
150
{
160
- return _container . Remove ( key ) ;
151
+ return _container . Remove ( key , out _ ) ;
152
+ }
153
+ }
154
+
155
+ /// <summary>Removes a key and its value from the table, and returns the removed value if it was present.</summary>
156
+ /// <param name="key">The key to remove.</param>
157
+ /// <param name="value">value removed from the table, if it was present.</param>
158
+ /// <returns><see langword="true"/> if the key is found and removed; otherwise, <see langword="false"/>.</returns>
159
+ /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
160
+ public bool Remove ( TKey key , [ MaybeNullWhen ( false ) ] out TValue value )
161
+ {
162
+ if ( key is null )
163
+ {
164
+ ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
165
+ }
166
+
167
+ lock ( _lock )
168
+ {
169
+ return _container . Remove ( key , out value ) ;
161
170
}
162
171
}
163
172
@@ -692,17 +701,19 @@ internal void RemoveAllKeys()
692
701
}
693
702
694
703
/// <summary>Removes the specified key from the table, if it exists.</summary>
695
- internal bool Remove ( TKey key )
704
+ internal bool Remove ( TKey key , [ MaybeNullWhen ( false ) ] out TValue value )
696
705
{
697
706
VerifyIntegrity ( ) ;
698
707
699
- int entryIndex = FindEntry ( key , out _ ) ;
708
+ int entryIndex = FindEntry ( key , out object ? valueObject ) ;
700
709
if ( entryIndex != - 1 )
701
710
{
702
711
RemoveIndex ( entryIndex ) ;
712
+ value = Unsafe . As < TValue > ( valueObject ) ;
703
713
return true ;
704
714
}
705
715
716
+ value = null ;
706
717
return false ;
707
718
}
708
719
0 commit comments