My source code:
# $arr = @(); results in same behaviour
$arr = New-Object System.Collections.ArrayList;
$arr.Count;
$arr += "z";
$arr.Count;
$arr.Clear();
$arr.Count;
Output:
0
1
1
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Powershell does some array-casting trickery when you do
+=, so the easy solution is to do$arr.Add("z"). Then$arr.Clear()will act like you expect.To clarify:
@()is a Powershell array. It uses+=, but you can’tClearit. (You can, however, do$arr = @()again to reset it to an empty array.)ArrayListis the .NET collection. It uses.Add, and you canClearit, but for some reason if you+=it, Powershell does some weird array coercion. (If any experts care to comment on this, awesome.)