Skip to content

Commit abc038c

Browse files
CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method
1 parent a41a58d commit abc038c

File tree

4 files changed

+6
-11
lines changed

4 files changed

+6
-11
lines changed

src/NUnitCommon/nunit.agent.core/Drivers/NUnitFrameworkApi2009.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public string Load(string testAssemblyPath, IDictionary<string, object> settings
5555
_testAssemblyPath = testAssemblyPath;
5656

5757
// Normally, the caller should check for an invalid requested runtime, but we make sure here
58-
var requestedRuntime = settings.ContainsKey(EnginePackageSettings.RequestedRuntimeFramework)
59-
? settings[EnginePackageSettings.RequestedRuntimeFramework] : null;
58+
settings.TryGetValue(EnginePackageSettings.RequestedRuntimeFramework, out object? requestedRuntime);
6059

6160
var idPrefix = _driverId + "-";
6261

src/NUnitCommon/nunit.extensibility/ExtensionNode.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public IEnumerable<string> PropertyNames
8080
/// <returns>A collection of values</returns>
8181
public IEnumerable<string> GetValues(string name)
8282
{
83-
if (_properties.ContainsKey(name))
84-
return _properties[name];
83+
if (_properties.TryGetValue(name, out List<string>? value))
84+
return value;
8585
else
8686
return Enumerable.Empty<string>();
8787
}

src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ private void ValidatePackageSettings()
377377
{
378378
var oldKey = entry.OldKey;
379379
var newKey = entry.NewKey;
380-
if (TestPackage.Settings.ContainsKey(oldKey) && !TestPackage.Settings.ContainsKey(newKey))
381-
TestPackage.Settings[newKey] = TestPackage.Settings[oldKey];
380+
if (TestPackage.Settings.TryGetValue(oldKey, out object? value) && !TestPackage.Settings.ContainsKey(newKey))
381+
TestPackage.Settings[newKey] = value;
382382
}
383383
}
384384
#else

src/NUnitEngine/nunit.engine/Services/ServiceManager.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ public class ServiceManager : IDisposable
2323

2424
public IService GetService( Type serviceType )
2525
{
26-
IService? theService = null;
27-
28-
if (_serviceIndex.ContainsKey(serviceType))
29-
theService = _serviceIndex[serviceType];
30-
else
26+
if (!_serviceIndex.TryGetValue(serviceType, out IService? theService))
3127
foreach( IService service in _services )
3228
{
3329
if( serviceType.IsInstanceOfType( service ) )

0 commit comments

Comments
 (0)