Skip to content

Commit 8ec6c27

Browse files
NameValueListBase key "not null" constraint removed (#4862)
Fixes #4861
1 parent 125b7a8 commit 8ec6c27

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

Source/Csla/NameValueListBase.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public abstract class NameValueListBase<[DynamicallyAccessedMembers(DynamicallyA
3030
ICloneable,
3131
IDataPortalTarget,
3232
IUseApplicationContext
33-
where K: notnull
3433
where V: notnull
3534
{
3635
/// <summary>
@@ -56,14 +55,10 @@ ApplicationContext IUseApplicationContext.ApplicationContext
5655
/// specified key.
5756
/// </summary>
5857
/// <param name="key">Key value for which to retrieve a value.</param>
59-
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
6058
public V? Value(K key)
6159
{
62-
if(key is null)
63-
throw new ArgumentNullException(nameof(key));
64-
6560
foreach (NameValuePair item in this)
66-
if (item.Key.Equals(key))
61+
if (KeysEqual(item.Key, key))
6762
return item.Value;
6863
return default(V);
6964
}
@@ -91,14 +86,10 @@ ApplicationContext IUseApplicationContext.ApplicationContext
9186
/// specified key.
9287
/// </summary>
9388
/// <param name="key">Key value for which to search.</param>
94-
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
9589
public bool ContainsKey(K key)
9690
{
97-
if (key is null)
98-
throw new ArgumentNullException(nameof(key));
99-
10091
foreach (NameValuePair item in this)
101-
if (item.Key.Equals(key))
92+
if (KeysEqual(item.Key, key))
10293
return true;
10394
return false;
10495
}
@@ -154,15 +145,11 @@ public bool ContainsValue(V value)
154145
/// Key to search for in the list.
155146
/// </param>
156147
/// <returns>Item from the list.</returns>
157-
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
158148
public NameValuePair? GetItemByKey(K key)
159149
{
160-
if (key is null)
161-
throw new ArgumentNullException(nameof(key));
162-
163150
foreach (NameValuePair item in this)
164151
{
165-
if (item != null && item.Key.Equals(key))
152+
if (KeysEqual(item.Key, key))
166153
{
167154
return item;
168155
}
@@ -171,6 +158,11 @@ public bool ContainsValue(V value)
171158

172159
}
173160

161+
private static bool KeysEqual(K key1, K key2)
162+
{
163+
return EqualityComparer<K>.Default.Equals(key1, key2);
164+
}
165+
174166
#endregion
175167

176168
/// <summary>
@@ -227,11 +219,9 @@ public NameValuePair()
227219
/// </summary>
228220
/// <param name="key">The key.</param>
229221
/// <param name="value">The value.</param>
230-
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
222+
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
231223
public NameValuePair(K key, V value)
232224
{
233-
if (key is null)
234-
throw new ArgumentNullException(nameof(key));
235225
if (value is null)
236226
throw new ArgumentNullException(nameof(value));
237227

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Csla.Test.Basic;
2+
3+
/// <summary>
4+
/// Example that a nullable key is now possible for <see cref="NameValueListBase{K,V}"/>.
5+
/// </summary>
6+
public class NullableKeyValueList : NameValueListBase<int?, string>
7+
{
8+
[Fetch]
9+
private void Fetch()
10+
{
11+
// This class must be compilable.
12+
}
13+
}

0 commit comments

Comments
 (0)