From 9ace0c679785a704f8553ec71cffe3add388faaa Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 1 Jul 2020 08:32:42 +0200 Subject: [PATCH] [Foundation] Fix code to not cause a potential null error when compiling using the .NET 5 BCL. (#8978) Fix code to not cause a potential null reference error from csc when compiling using the .NET 5 BCL: > Foundation/DictionaryContainer.cs(330,47): error CS8604: Possible null reference argument for parameter 'source' in 'IEnumerable Enumerable.Select(IEnumerable source, Func selector)'. And do so by rewriting Linq code, which also makes the code much less memory hungry and more performant. --- src/Foundation/DictionaryContainer.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Foundation/DictionaryContainer.cs b/src/Foundation/DictionaryContainer.cs index e4ce8f3efd..4466eb4559 100644 --- a/src/Foundation/DictionaryContainer.cs +++ b/src/Foundation/DictionaryContainer.cs @@ -27,7 +27,6 @@ // using System; -using System.Linq; using System.Runtime.InteropServices; #if !COREBUILD @@ -326,8 +325,12 @@ namespace Foundation { protected void SetArrayValue (NSString key, T[]? values) { - if (NullCheckAndRemoveKey (key, values == null)) - Dictionary [key] = NSArray.FromNSObjects (values.Select (x => NSObject.FromObject (x)).ToArray ()); + if (NullCheckAndRemoveKey (key, values == null)) { + var nsValues = new NSObject [values!.Length]; + for (var i = 0; i < values.Length; i++) + nsValues [i] = NSObject.FromObject (values [i]); + Dictionary [key] = NSArray.FromNSObjects (nsValues); + } } protected void SetArrayValue (NSString key, string[]? values)