diff --git a/src/NuGet.Clients/NuGet.CommandLine/Commands/Pack/AssemblyMetadataExtractor.cs b/src/NuGet.Clients/NuGet.CommandLine/Commands/Pack/AssemblyMetadataExtractor.cs index 410be6345b3..722fda1ed14 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/Commands/Pack/AssemblyMetadataExtractor.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/Commands/Pack/AssemblyMetadataExtractor.cs @@ -82,9 +82,9 @@ public void ExtractMetadata(PackageBuilder builder, string assemblyPath) if (!builder.Authors.Any()) { - if (assemblyMetadata.Properties.ContainsKey("authors")) + if (assemblyMetadata.Properties.TryGetValue("authors", out var authors)) { - builder.Authors.AddRange(assemblyMetadata.Properties["authors"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); + builder.Authors.AddRange(authors.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); } else if (!string.IsNullOrEmpty(assemblyMetadata.Company)) { @@ -92,18 +92,18 @@ public void ExtractMetadata(PackageBuilder builder, string assemblyPath) } } - if (assemblyMetadata.Properties.ContainsKey("owners")) + if (assemblyMetadata.Properties.TryGetValue("owners", out var owners)) { - builder.Owners.AddRange(assemblyMetadata.Properties["owners"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); + builder.Owners.AddRange(owners.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); } builder.Properties.AddRange(assemblyMetadata.Properties); // Let the id be overriden by AssemblyMetadataAttribute - // This preserves the existing behavior if no id metadata + // This preserves the existing behavior if no id metadata // is provided by the assembly. - if (builder.Properties.ContainsKey("id")) + if (builder.Properties.TryGetValue("id", out var id)) { - builder.Id = builder.Properties["id"]; + builder.Id = id; } else { @@ -174,10 +174,10 @@ private static Dictionary GetProperties(IList(StringComparer.OrdinalIgnoreCase); // NOTE: we make this check only by attribute type fullname, and we try to duck // type it, therefore enabling the same metadata extesibility behavior for other platforms - // that don't define the attribute already as part of the framework. - // A package author could simply declare this attribute in his own project, using - // the same namespace and members, and we'd pick it up automatically. This is consistent - // with what MS did in the past with the System.Runtime.CompilerServices.ExtensionAttribute + // that don't define the attribute already as part of the framework. + // A package author could simply declare this attribute in his own project, using + // the same namespace and members, and we'd pick it up automatically. This is consistent + // with what MS did in the past with the System.Runtime.CompilerServices.ExtensionAttribute // which allowed Linq to be re-implemented for .NET 2.0 :). var attributeName = typeof(AssemblyMetadataAttribute).FullName; foreach (var attribute in attributes.Where(x => diff --git a/src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs b/src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs index 4e571bffd91..75907b4a978 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs @@ -396,9 +396,9 @@ public string InitializeProperties(Packaging.IPackageMetadata metadata) _properties.Clear(); // Allow Id to be overriden by cmd line properties - if (ProjectProperties.ContainsKey("Id")) + if (ProjectProperties.TryGetValue("Id", out var id)) { - _properties.Add("Id", ProjectProperties["Id"]); + _properties.Add("Id", id); } else { @@ -776,9 +776,9 @@ private PackageDependency CreateDependencyFromProject(dynamic project, Dictionar } VersionRange versionRange = null; - if (dependencies.ContainsKey(builder.Id)) + if (dependencies.TryGetValue(builder.Id, out PackageDependency dependency)) { - VersionRange nuspecVersion = dependencies[builder.Id].VersionRange; + VersionRange nuspecVersion = dependency.VersionRange; if (nuspecVersion != null) { versionRange = nuspecVersion; diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs b/src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs index 6d32a0a4f45..eaf370bf538 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs @@ -76,9 +76,9 @@ public PackArgs GetPackArgs(IPackTaskRequest request) if (request.NuspecProperties != null && request.NuspecProperties.Any()) { packArgs.Properties.AddRange(ParsePropertiesAsDictionary(request.NuspecProperties)); - if (packArgs.Properties.ContainsKey("version")) + if (packArgs.Properties.TryGetValue("version", out var version)) { - packArgs.Version = packArgs.Properties["version"]; + packArgs.Version = version; } } } diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Why/DependencyGraphFinder.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Why/DependencyGraphFinder.cs index 80f6681c74b..82d98f300f2 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Why/DependencyGraphFinder.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Why/DependencyGraphFinder.cs @@ -166,9 +166,7 @@ internal static class DependencyGraphFinder } } - return dependencyNodes.ContainsKey(topLevelPackage) - ? dependencyNodes[topLevelPackage] - : null; + return dependencyNodes.GetValueOrDefault(topLevelPackage); } /// diff --git a/src/NuGet.Core/NuGet.Configuration/PackageSourceMapping/SearchTree.cs b/src/NuGet.Core/NuGet.Configuration/PackageSourceMapping/SearchTree.cs index 326a21a38d7..f87c9efed8d 100644 --- a/src/NuGet.Core/NuGet.Configuration/PackageSourceMapping/SearchTree.cs +++ b/src/NuGet.Core/NuGet.Configuration/PackageSourceMapping/SearchTree.cs @@ -128,12 +128,12 @@ public IReadOnlyList GetConfiguredPackageSources(string term) { char c = term[i]; - if (!currentNode.Children.ContainsKey(c)) + if (!currentNode.Children.TryGetValue(c, out SearchNode? child)) { break; } - currentNode = currentNode.Children[c]; + currentNode = child; if (currentNode.IsGlobbing) { @@ -172,7 +172,7 @@ public IReadOnlyList GetConfiguredPackageSources(string term) { char c = term[i]; - if (!currentNode.Children.ContainsKey(c)) + if (!currentNode.Children.TryGetValue(c, out SearchNode? child)) { if (!currentNode.IsGlobbing) { @@ -181,7 +181,7 @@ public IReadOnlyList GetConfiguredPackageSources(string term) break; } - currentNode = currentNode.Children[c]; + currentNode = child; sb.Append(c); } diff --git a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs index a0a795c2cc1..784f8851742 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs @@ -2964,15 +2964,13 @@ internal async Task> PreviewBuildIntegratedProjectsA } else { - if (!nugetProjectActionsLookup.ContainsKey(buildIntegratedProject.MSBuildProjectPath)) + if (!nugetProjectActionsLookup.TryGetValue(buildIntegratedProject.MSBuildProjectPath, out nuGetProjectActions)) { throw new ArgumentException( message: string.Format(CultureInfo.CurrentCulture, Strings.UnableToFindPathInLookupOrList, nameof(nugetProjectActionsLookup), buildIntegratedProject.MSBuildProjectPath, nameof(packageIdentity), nameof(primarySources)), paramName: nameof(nugetProjectActionsLookup)); } - nuGetProjectActions = nugetProjectActionsLookup[buildIntegratedProject.MSBuildProjectPath]; - if (nuGetProjectActions.Length == 0) { // Continue to next project if there are no actions for current project. @@ -3477,7 +3475,7 @@ await BuildIntegratedRestoreUtility.ExecuteInitPs1ScriptsAsync( if (dgSpecForParents.Restore.Count > 0) { // Restore and commit the lock file to disk regardless of the result - // This will restore all parents in a single restore + // This will restore all parents in a single restore await DependencyGraphRestoreUtility.RestoreAsync( dgSpecForParents, referenceContext,