-
Notifications
You must be signed in to change notification settings - Fork 554
[Foundation] Unify the FromObjectsAndKeys implementations for creating dictionaries. #24556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -129,6 +129,37 @@ internal static NSArray PickOdd (object f, object [] args) | |||||
| return NSArray.FromObjects (ret); | ||||||
| } | ||||||
|
|
||||||
| // Checks: | ||||||
| // * 'objects' and 'keys' for null | ||||||
| // * count isn't negative | ||||||
| // * count isn't higher than the number of elements in either array | ||||||
| // returns false if an empty dictionary can be returned | ||||||
| private protected static bool ValidateFromObjectsAndKeys<T, K> (T [] objects, K [] keys, nint count) | ||||||
| { | ||||||
| ArgumentNullException.ThrowIfNull (objects); | ||||||
| ArgumentNullException.ThrowIfNull (keys); | ||||||
|
|
||||||
| if (count < 0 || objects.Length < count || keys.Length < count) | ||||||
| throw new ArgumentException (nameof (count)); | ||||||
|
||||||
| throw new ArgumentException (nameof (count)); | |
| throw new ArgumentException ("count must be non-negative and not greater than the length of both arrays", nameof (count)); |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent use of array length in forwarding calls. This method uses keys.Length when forwarding to the count-based overload, while most other similar methods use objects.Length (e.g., NSMutableDictionary.cs lines 47, 61; NSDictionary_2.cs line 292; NSMutableDictionary_2.cs lines 287, 301). Since validation ensures both arrays have the same length, this doesn't affect functionality, but for maintainability and consistency, all forwarding calls should use the same pattern. Consider using objects.Length consistently across all implementations.
| return FromObjectsAndKeys (objects, keys, keys.Length); | |
| return FromObjectsAndKeys (objects, keys, objects.Length); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -256,13 +256,8 @@ static NSDictionary<TKey, TValue> GenericFromObjectsAndKeysInternal (NSArray obj | |||||
| /// <returns>A new dictionary containing the specified key-value pairs.</returns> | ||||||
| public static NSDictionary<TKey, TValue> FromObjectsAndKeys (TValue? [] objects, TKey [] keys, nint count) | ||||||
| { | ||||||
| ArgumentNullException.ThrowIfNull (objects); | ||||||
| ArgumentNullException.ThrowIfNull (keys); | ||||||
|
|
||||||
| if (objects.Length != keys.Length) | ||||||
| throw new ArgumentException (nameof (objects) + " and " + nameof (keys) + " arrays have different sizes"); | ||||||
| if (count < 1 || objects.Length < count) | ||||||
| throw new ArgumentException (nameof (count)); | ||||||
| if (!ValidateFromObjectsAndKeys (objects, keys, count)) | ||||||
| return new NSDictionary<TKey, TValue> (); | ||||||
|
|
||||||
| using (var no = NSArray.FromNativeObjects (objects, count)) | ||||||
| using (var nk = NSArray.FromNativeObjects (keys, count)) | ||||||
|
|
@@ -277,11 +272,8 @@ public static NSDictionary<TKey, TValue> FromObjectsAndKeys (TValue? [] objects, | |||||
| /// <returns>A new dictionary containing the specified key-value pairs.</returns> | ||||||
| public static NSDictionary<TKey, TValue> FromObjectsAndKeys (TValue? [] objects, TKey [] keys) | ||||||
| { | ||||||
| ArgumentNullException.ThrowIfNull (objects); | ||||||
| ArgumentNullException.ThrowIfNull (keys); | ||||||
|
|
||||||
| if (objects.Length != keys.Length) | ||||||
| throw new ArgumentException (nameof (objects) + " and " + nameof (keys) + " arrays have different sizes"); | ||||||
| if (!ValidateFromObjectsAndKeys (objects, keys)) | ||||||
| return new NSDictionary<TKey, TValue> (); | ||||||
|
|
||||||
| return FromObjectsAndKeys (objects, keys, keys.Length); | ||||||
|
||||||
| return FromObjectsAndKeys (objects, keys, keys.Length); | |
| return FromObjectsAndKeys (objects, keys, objects.Length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent exception type for negative count. This validation throws ArgumentException for negative count values, but ArgumentOutOfRangeException would be more appropriate and consistent with NSArray.FromNativeObjects which throws ArgumentOutOfRangeException for negative count (NSArray.cs line 238-239). Consider changing the exception type when count is negative to ArgumentOutOfRangeException, while keeping ArgumentException for other validation failures.