jonathanmedd / blog-comments

0 stars 0 forks source link

2015/07/powershell-default-value-of-inta-is-0 #8

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

PowerShell: Default Value of [Int]$a is 0 - Jonathan Medd's Blog

https://www.jonathanmedd.net/2015/07/powershell-default-value-of-inta-is-0.html

sidesw1pe commented 1 year ago

Not only that, but declaring a variable of type [string] will result in a default value of ''. Furthermore it is impossible to set these variables to $null once declared with a type. I encountered this yesterday and it was quite confusing, even ChatGPT was confused and couldn't give a straight answer.

PS C:\Users\john> Remove-Variable blah
PS C:\Users\john> [string]$blah = $null
PS C:\Users\john> $blah -eq $null
False
PS C:\Users\john> $blah = $null
PS C:\Users\john> $blah -eq $null
False
PS C:\Users\john>

I referred to the Microsoft documentation on variables, and it stated:

The default value of all variables is $null. To delete the value of a variable, change the value to $null.

But these statements from the documentation are untrue, if the variable is declared with a type. I'm a novice so I'm probably completely overlooking something, but anyway.