EliziumNet / Loopz

PowerShell iteration utilities with additional tools like the Parameter Set Tools
https://eliziumnet.github.io/Loopz/
MIT License
3 stars 0 forks source link

Review hashtable.Count access #118

Closed plastikfan closed 3 years ago

plastikfan commented 3 years ago

It has been discovered that when dealing with hashtables, it is best not to use the Count property on the hashtable as it is unreliable and can sometimes cause the following error:

Cannot compare "System.Collections.Hashtable" because it is not IComparable.

The better way to check this is to use the Count property on keys ie:

$hash.Keys.Count

not

$hash.Count

plastikfan commented 3 years ago

Raised an issue in PowerShell repo: Why is accessing Count property on Hashtable unreliable?

plastikfan commented 3 years ago

Actually, the best way going forward is to use PSBase to accesss a hashtable's properies in a reliable fashion. So:

$Hash.PSBase.Count

rather than

$Hash.Keys.Count

because even direct use of 'Keys' on Hashtable could fail, if the hashtable itself contains a key named 'Keys'.