Powershell數(shù)組一般具有多態(tài)性,如果你不指定元素的具體類型,解釋器會自動選擇合適的類型存儲每個元素。如果要統(tǒng)一限制所有元素的類型,可是使用類型名和一對方括號作為數(shù)組變量的類型。這樣每當(dāng)賦值時,會自動類型檢查。如果目標數(shù)據(jù)類型不能轉(zhuǎn)換成功,就會拋出一個異常。
PS C:Powershell> [int[]] $nums=@()PS C:Powershell> $nums+=2012PS C:Powershell> $nums+=12.3PS C:Powershell> $nums+="999"PS C:Powershell> $nums+="can not convert"Cannot convert value "can not convert" to type "System.Int32". Error: "Input string was not in a correct format."At line:1 char:6+ $nums <<<< +="can not convert" + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException
新聞熱點
疑難解答
圖片精選