今回は比較演算子でちょっとした実験を行ってみたいと思います。
比較演算子の説明は「比較演算子(PowerShell 入門)」を参照願います。
下記のように数値の2と2を-eq演算子で比較した場合、当然これは等しいのでTrueが返されます。
PS > 2 -eq 2 True
では、次のように数値の2と文字列"2"を比較したらどうなるでしょうか?
PS > 2 -eq "2"
この結果はTrueとなります。
ではなぜTrueとなるのでしょうか?
これはPowerShellがスカラー比較を行うためです。(スカラー比較とは2も"2"も同一視すること)
では、下記のようにキャストして比較してみたらどうなるか
PS %gt; [int]2 -eq [string]"2" True
これも結果としては同じ値と見なされてしまい、Trueを返します。
下記のように数値型と文字列型(GetType()メソッドは型を返します)の比較はスカラー比較は適用できないため、Falseが返されます。
PS > (2).GetType() -eq "2".GetType() False
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail