Skip to content

Commit 35c5b7d

Browse files
committed
Use Dictionary.TryGetValue to avoid double lookup
1 parent 1a779d3 commit 35c5b7d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/HtmlAgilityPack.Shared/HtmlDocument.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,9 @@ public HtmlNode GetElementbyId(string id)
694694
throw new Exception(HtmlExceptionUseIdAttributeFalse);
695695
}
696696

697-
return Nodesid.ContainsKey(id) ? Nodesid[id] : null;
697+
return Nodesid.TryGetValue(id, out HtmlNode value)
698+
? value
699+
: null;
698700
}
699701

700702
/// <summary>

src/HtmlAgilityPack.Shared/NameValuePairList.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ internal List<KeyValuePair<string, string>> GetNameValuePairs(string name)
5050
{
5151
if (name == null)
5252
return _allPairs;
53-
return _pairsWithName.ContainsKey(name) ? _pairsWithName[name] : new List<KeyValuePair<string, string>>();
53+
54+
return _pairsWithName.TryGetValue(name, out List<KeyValuePair<string, string>> value)
55+
? value
56+
: new List<KeyValuePair<string, string>>();
5457
}
5558

5659
internal string GetNameValuePairValue(string name)

0 commit comments

Comments
 (0)