-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Prerequisites
- Existing Issue: Search the existing issues for this repository. If there is an issue that fits your needs do not file a new one. Subscribe, react, or comment on that issue instead.
- Descriptive Title: Write the title for this issue as a short synopsis. If possible, provide context. For example, "Typo in
Get-Foocmdlet" instead of "Typo." - Verify Version: If there is a mismatch between documentation and the behavior on your system, ensure that the version you are using is the same as the documentation. Check this box if they match or the issue you are reporting is not version specific.
Links
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_intrinsic_members#properties
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_properties#examples
Summary
The documentation makes the following incorrect assertions:
The Count and Length properties are available to all PowerShell objects, not just collections.
All collections have a Count property that returns the number of objects in the collection.
PowerShell makes the intrinsic Count/Length properties available to scalar objects only. The properties are not made available intrinsicly to objects whose type is considered a collection by PS.
As not all collections have their own type-native Count or Length (or either), these properties are not available to all PowerShell objects/all collections.
Details
PS's rules/special-casing for what it considers a collection/scalar can be found here and here.
This comment contains code to check for the absence of Count and Length properties with commonly used collection types.
Suggested Fix
Make the following adjustments to about_Intrinsic_Members and about_Properties:
- Remove the assertions that
Count/Lengthproperties are available to all objects and to all collections. - State that
Count/Lengthproperties are intrinsicly made available by PS if:- PS considers the object scalar.
- The scalar object does not already have a
Count/Lengthproperty.
- Note that
$nullis scalar and has an intrinsicCount/Lengthof0. Likewise, an uninitialized variable is implicitly$null, so it too has an intrinsicCount/Lengthof0. - State that aside from arrays, a PS user will typically encounter collections with only a
Countproperty (noLength).
In addition:
- Encourage the use of
CountoverLengthas a general means to retrieve the number of elements in a collection. See Recommend usingCountto retrieve the number of elements in an in-memory collection #11614.