Skip to content

Commit 38f5287

Browse files
committed
Tweaks
1 parent 7f9f0c3 commit 38f5287

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/Foundation/NSMutableDictionary_2.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,11 @@ public bool ContainsKey (TKey key)
237237
/// Gets or sets the value associated with the specified key.
238238
/// </summary>
239239
/// <param name="index">The key of the value to get or set.</param>
240-
/// <returns>The value associated with the specified key.</returns>
241-
public TValue this [TKey index] {
240+
/// <returns>The value associated with the specified key, or <see langword="null" /> if the key wasn't found.</returns>
241+
[DisallowNull] // don't allow setting null values
242+
public TValue? this [TKey index] {
242243
get {
243-
var result = ObjectForKey (index);
244-
if (result is null)
245-
throw new KeyNotFoundException ();
246-
return result;
244+
return ObjectForKey (index);
247245
}
248246
set {
249247
Add (index, value);
@@ -368,15 +366,16 @@ bool IDictionary<TKey, TValue>.Remove (TKey key)
368366
return Remove (key);
369367
}
370368

371-
bool IDictionary<TKey, TValue>.TryGetValue (TKey key, [MaybeNullWhen (false)] out TValue value)
369+
bool IDictionary<TKey, TValue>.TryGetValue (TKey key, [NotNullWhen (true)] out TValue? value)
372370
{
373-
var result = TryGetValue (key, out var nullableValue);
374-
value = nullableValue!;
375-
return result;
371+
return TryGetValue (key, out value);
376372
}
377373

378-
TValue IDictionary<TKey, TValue>.this [TKey index] {
374+
[DisallowNull] // don't allow setting null values
375+
TValue? IDictionary<TKey, TValue>.this [TKey index] {
376+
#pragma warning disable CS8768 // Nullability of reference types in return type doesn't match implemented member 'TValue IDictionary<TKey, TValue>.this[TKey key].get'
379377
get {
378+
#pragma warning restore CS8768
380379
return this [index];
381380
}
382381
set {

tests/monotouch-test/Foundation/NSMutableDictionary2Test.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,18 @@ public void IndexerTest ()
358358
}
359359

360360
[Test]
361-
public void IndexerGetterThrowsKeyNotFoundTest ()
361+
public void IndexerGetterKeyNotFoundBehaviorTest ()
362362
{
363363
var value1 = NSDate.FromTimeIntervalSinceNow (1);
364364
var key1 = new NSString ("key1");
365365
var keyMissing = new NSString ("missing");
366366

367367
var dict = new NSMutableDictionary<NSString, NSDate> (key1, value1);
368368

369-
// Accessing via the indexer property should throw KeyNotFoundException for missing keys
370-
Assert.Throws<KeyNotFoundException> (() => GC.KeepAlive (dict [keyMissing]), "missing key");
369+
// Accessing via the indexer property should return null
370+
Assert.IsNull (dict [keyMissing], "missing key");
371371

372-
// But accessing via IDictionary interface should return null
372+
// Accessing via IDictionary interface should return null too
373373
IDictionary<NSString, NSDate> idict = dict;
374374
Assert.IsNull (idict [keyMissing], "missing key via interface");
375375
}
@@ -399,10 +399,10 @@ public void MissingKeyAccessTest ()
399399
// ContainsKey should return false for missing keys
400400
Assert.IsFalse (dict.ContainsKey (keyMissing), "ContainsKey missing");
401401

402-
// Indexer getter should throw KeyNotFoundException
403-
Assert.Throws<KeyNotFoundException> (() => GC.KeepAlive (dict [keyMissing]), "Indexer missing");
402+
// Indexer getter should return null
403+
Assert.IsNull (dict [keyMissing], "Indexer missing");
404404

405-
// But IDictionary indexer should return null
405+
// IDictionary indexer should also return null
406406
IDictionary<NSString, NSDate> idict = dict;
407407
Assert.IsNull (idict [keyMissing], "IDictionary indexer missing");
408408
}
@@ -421,7 +421,7 @@ public void EmptyDictionaryMissingKeyTest ()
421421
Assert.IsFalse (dict.TryGetValue (keyMissing, out value), "TryGetValue");
422422
Assert.IsNull (value, "TryGetValue out");
423423

424-
Assert.Throws<KeyNotFoundException> (() => GC.KeepAlive (dict [keyMissing]), "Indexer");
424+
Assert.IsNull (dict [keyMissing], "Indexer");
425425

426426
IDictionary<NSString, NSDate> idict = dict;
427427
Assert.IsNull (idict [keyMissing], "IDictionary indexer");

0 commit comments

Comments
 (0)