diff --git a/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs b/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs index b6f89e55d531..545ece0efa04 100644 --- a/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs +++ b/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs @@ -28,9 +28,9 @@ public IEnumerable ConvertFromString(string value, ILContext contex //Device.GetNamedSize(namedSize, targetObject.GetType()) yield return Instruction.Create(OpCodes.Ldc_I4, (int)namedSize); var parent = node.Parent as IElementNode; - if (parent != null && context.Variables.ContainsKey(parent)) + if (parent != null && context.Variables.TryGetValue(parent, out VariableDefinition parentValue)) { - yield return Instruction.Create(OpCodes.Ldloc, context.Variables[parent]); + yield return Instruction.Create(OpCodes.Ldloc, parentValue); yield return Instruction.Create(OpCodes.Callvirt, module.ImportMethodReference( context.Cache, module.TypeSystem.Object, diff --git a/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs b/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs index 543a8f4bbe80..a0ece490f3ee 100644 --- a/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs +++ b/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs @@ -96,8 +96,9 @@ public void Visit(ElementNode node, INode parentNode) MethodDefinition factoryMethodInfo = null; MethodDefinition parameterizedCtorInfo = null; MethodDefinition ctorInfo = null; - - if (node.Properties.ContainsKey(XmlName.xArguments) && !node.Properties.ContainsKey(XmlName.xFactoryMethod)) + + INode factoryMethodNode = null; + if (node.Properties.ContainsKey(XmlName.xArguments) && !node.Properties.TryGetValue(XmlName.xFactoryMethod, out factoryMethodNode)) { factoryCtorInfo = typedef.AllMethods(Context.Cache).FirstOrDefault(md => md.methodDef.IsConstructor && !md.methodDef.IsStatic && @@ -107,9 +108,9 @@ public void Visit(ElementNode node, INode parentNode) if (!typedef.IsValueType) //for ctor'ing typedefs, we first have to ldloca before the params Context.IL.Append(PushCtorXArguments(factoryCtorInfo.ResolveGenericParameters(typeref, Module), node)); } - else if (node.Properties.ContainsKey(XmlName.xFactoryMethod)) + else if (factoryMethodNode != null) { - var factoryMethod = (string)(node.Properties[XmlName.xFactoryMethod] as ValueNode).Value; + var factoryMethod = (string)(factoryMethodNode as ValueNode).Value; factoryMethodInfo = typedef.AllMethods(Context.Cache).FirstOrDefault(md => !md.methodDef.IsConstructor && md.methodDef.Name == factoryMethod && md.methodDef.IsStatic && @@ -317,14 +318,14 @@ IEnumerable PushCtorArguments(MethodReference ctorinfo, ElementNode IEnumerable PushCtorXArguments(MethodReference factoryCtorInfo, ElementNode enode) { - if (!enode.Properties.ContainsKey(XmlName.xArguments)) + if (!enode.Properties.TryGetValue(XmlName.xArguments, out INode value)) yield break; var arguments = new List(); - var node = enode.Properties[XmlName.xArguments] as ElementNode; + var node = value as ElementNode; if (node != null) arguments.Add(node); - var list = enode.Properties[XmlName.xArguments] as ListNode; + var list = value as ListNode; if (list != null) { foreach (var n in list.CollectionItems) diff --git a/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs b/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs index 8c44720d5363..432b6c22c3d7 100644 --- a/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs +++ b/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs @@ -8,15 +8,15 @@ static class MethodDefinitionExtensions { public static bool MatchXArguments(this MethodDefinition methodDef, ElementNode enode, TypeReference declaringTypeRef, ModuleDefinition module, ILContext context) { - if (!enode.Properties.ContainsKey(XmlName.xArguments)) + if (!enode.Properties.TryGetValue(XmlName.xArguments, out INode value)) return !methodDef.HasParameters; var arguments = new List(); - var node = enode.Properties[XmlName.xArguments] as ElementNode; + var node = value as ElementNode; if (node != null) arguments.Add(node); - var list = enode.Properties[XmlName.xArguments] as ListNode; + var list = value as ListNode; if (list != null) foreach (var n in list.CollectionItems) arguments.Add(n); diff --git a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs index 15f021d26bda..1f119173dc47 100644 --- a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs +++ b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs @@ -1499,9 +1499,9 @@ static bool CanAddToResourceDictionary(VariableDefinition parent, TypeReference && !collectionType.InheritsFromOrImplements(context.Cache, context.Module.ImportReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "ResourceDictionary")))) return false; - if (node.Properties.ContainsKey(XmlName.xKey)) + if (node.Properties.TryGetValue(XmlName.xKey, out INode nodePropertyValue)) { - var valueNode = node.Properties[XmlName.xKey] as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); + var valueNode = nodePropertyValue as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); var key = (valueNode).Value as string; var names = context.Cache.GetResourceNamesInUse(parent); if (names.Contains(key)) @@ -1552,10 +1552,10 @@ static IEnumerable AddToResourceDictionary(VariableDefinition paren { var module = context.Body.Method.Module; - if (node.Properties.ContainsKey(XmlName.xKey)) + if (node.Properties.TryGetValue(XmlName.xKey, out INode nodePropertyValue)) { var names = context.Cache.GetResourceNamesInUse(parent); - var valueNode = node.Properties[XmlName.xKey] as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); + var valueNode = nodePropertyValue as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); var key = (valueNode).Value as string; names.Add(key); diff --git a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs index 8468cf0f16d6..90336b44e067 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs @@ -350,10 +350,9 @@ protected virtual void OnShellSectionPropertyChanged(object sender, PropertyChan _currentContent = newContent; _currentIndex = newIndex; - if (!_renderers.ContainsKey(newContent)) + if (!_renderers.TryGetValue(newContent, out var currentRenderer)) return; - var currentRenderer = _renderers[newContent]; _isAnimatingOut = oldRenderer; _pageAnimation?.StopAnimation(true); _pageAnimation = null; diff --git a/src/Controls/src/Core/Internals/NameScope.cs b/src/Controls/src/Core/Internals/NameScope.cs index d86031f406c6..f11fb29d6629 100644 --- a/src/Controls/src/Core/Internals/NameScope.cs +++ b/src/Controls/src/Core/Internals/NameScope.cs @@ -52,10 +52,10 @@ void INameScope.UnregisterName(string name) if (name == "") throw new ArgumentException("name was provided as empty string.", nameof(name)); - if (!_names.ContainsKey(name)) + if (!_names.TryGetValue(name, out var nameValue)) throw new ArgumentException("name provided had not been registered.", nameof(name)); - _values.Remove(_names[name]); + _values.Remove(nameValue); _names.Remove(name); } } diff --git a/src/Controls/src/Core/MessagingCenter.cs b/src/Controls/src/Core/MessagingCenter.cs index bb14b25aaba7..c9942a9f7076 100644 --- a/src/Controls/src/Core/MessagingCenter.cs +++ b/src/Controls/src/Core/MessagingCenter.cs @@ -202,10 +202,10 @@ void InnerSend(string message, Type senderType, Type argType, object sender, obj if (message == null) throw new ArgumentNullException(nameof(message)); var key = new Sender(message, senderType, argType); - if (!_subscriptions.ContainsKey(key)) + if (!_subscriptions.TryGetValue(key, out List subscriptions)) return; - List subcriptions = _subscriptions[key]; - if (subcriptions == null || !subcriptions.Any()) + + if (subscriptions == null || !subscriptions.Any()) return; // should not be reachable // ok so this code looks a bit funky but here is the gist of the problem. It is possible that in the course @@ -213,10 +213,10 @@ void InnerSend(string message, Type senderType, Type argType, object sender, obj // the callback. This would invalidate the enumerator. To work around this we make a copy. However if you unsubscribe // from a message you can fairly reasonably expect that you will therefor not receive a call. To fix this we then // check that the item we are about to send the message to actually exists in the live list. - List subscriptionsCopy = subcriptions.ToList(); + List subscriptionsCopy = subscriptions.ToList(); foreach (Subscription subscription in subscriptionsCopy) { - if (subscription.Subscriber.Target != null && subcriptions.Contains(subscription)) + if (subscription.Subscriber.Target != null && subscriptions.Contains(subscription)) { subscription.InvokeCallback(sender, args); } @@ -229,9 +229,9 @@ void InnerSubscribe(object subscriber, string message, Type senderType, Type arg throw new ArgumentNullException(nameof(message)); var key = new Sender(message, senderType, argType); var value = new Subscription(subscriber, target, methodInfo, filter); - if (_subscriptions.ContainsKey(key)) + if (_subscriptions.TryGetValue(key, out List subscriptions)) { - _subscriptions[key].Add(value); + subscriptions.Add(value); } else { @@ -248,10 +248,10 @@ void InnerUnsubscribe(string message, Type senderType, Type argType, object subs throw new ArgumentNullException(nameof(message)); var key = new Sender(message, senderType, argType); - if (!_subscriptions.ContainsKey(key)) + if (!_subscriptions.TryGetValue(key, out List subscriptions)) return; - _subscriptions[key].RemoveAll(sub => sub.CanBeRemoved() || sub.Subscriber.Target == subscriber); - if (!_subscriptions[key].Any()) + subscriptions.RemoveAll(sub => sub.CanBeRemoved() || sub.Subscriber.Target == subscriber); + if (!subscriptions.Any()) _subscriptions.Remove(key); } diff --git a/src/Controls/src/Core/ResourceDictionary.cs b/src/Controls/src/Core/ResourceDictionary.cs index 5cb81399d0df..72bcd2dec286 100644 --- a/src/Controls/src/Core/ResourceDictionary.cs +++ b/src/Controls/src/Core/ResourceDictionary.cs @@ -190,10 +190,10 @@ public object this[string index] { get { - if (_innerDictionary.ContainsKey(index)) - return _innerDictionary[index]; - if (_mergedInstance != null && _mergedInstance.ContainsKey(index)) - return _mergedInstance[index]; + if (_innerDictionary.TryGetValue(index, out var innerValue)) + return innerValue; + if (_mergedInstance != null && _mergedInstance.TryGetValue(index, out var mergedValue)) + return mergedValue; if (_mergedDictionaries != null) { var dictionaries = (ObservableCollection)MergedDictionaries; diff --git a/src/Controls/src/Core/ResourcesExtensions.cs b/src/Controls/src/Core/ResourcesExtensions.cs index 32673eaba4c6..c2393d2bf40f 100644 --- a/src/Controls/src/Core/ResourcesExtensions.cs +++ b/src/Controls/src/Core/ResourcesExtensions.cs @@ -35,11 +35,11 @@ public static IEnumerable> GetMergedResources(this { resources = resources ?? new Dictionary(8, StringComparer.Ordinal); foreach (KeyValuePair res in app.SystemResources) - if (!resources.ContainsKey(res.Key)) + if (!resources.TryGetValue(res.Key, out var resourceValue)) resources.Add(res.Key, res.Value); else if (res.Key.StartsWith(Style.StyleClassPrefix, StringComparison.Ordinal)) { - var mergedClassStyles = new List