{"id":10001,"date":"2006-07-15T13:29:00","date_gmt":"2006-07-15T13:29:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/15\/cascading-type-casts\/"},"modified":"2019-02-18T13:21:32","modified_gmt":"2019-02-18T20:21:32","slug":"cascading-type-casts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/cascading-type-casts\/","title":{"rendered":"Cascading Type Casts"},"content":{"rendered":"<p>A user on the newsgroup wondered whether the example below highlighted a bug or a feature.&nbsp; They applied multiple type declarations on a single parameter and Windows PowerShell did not complain.<\/p>\n<p><font face=\"Courier New\" size=\"1\">function check { <br \/>&nbsp; param([string] [int] $y = 0, $x = 0)<br \/>###^^^^^^^^^^^^<br \/>&nbsp; $x.GetType().ToString();<br \/>&nbsp; $y.GetType().ToString();<br \/>&nbsp; $x + $y;<br \/>}<\/font><\/p>\n<p>That&#8217;s no bug, that&#8217;s a feature.&nbsp; \ud83d\ude42<\/p>\n<p>This is an example of chained type casting.&nbsp; There are all sorts of examples where this is useful.&nbsp; Consider the following:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong>&#8220;PowerShell&#8221;<br \/><\/strong>PowerShell<br \/>PS&gt; <strong>[int[]]&#8221;PowerShell&#8221;<br \/><\/strong><font color=\"#ff0000\">Cannot convert value &#8220;PowerShell&#8221; to type &#8220;System.Int32[]&#8221;. Error: &#8220;Invalid<br \/>&nbsp;cast from &#8216;System.String&#8217; to &#8216;System.Int32[]&#8217;.&#8221;<br \/>At line:1 char:8<br \/>+ [int[]]&#8221; &lt;&lt;&lt;&lt; PowerShell&#8221;<br \/><\/font>PS&gt; <strong>[char[]]&#8221;PowerShell&#8221;<br \/><\/strong>P<br \/>o<br \/>w<br \/>e<br \/>r<br \/>S<br \/>h<br \/>e<br \/>l<br \/>l<br \/>PS&gt; <strong>[int[]][char[]]&#8221;PowerShell&#8221;<br \/><\/strong>80<br \/>111<br \/>119<br \/>101<br \/>114<br \/>83<br \/>104<br \/>101<br \/>108<br \/>108<br \/>PS&gt; <strong>[char[]][int[]][char[]]&#8221;PowerShell&#8221;<br \/><\/strong>P<br \/>o<br \/>w<br \/>e<br \/>r<br \/>S<br \/>h<br \/>e<br \/>l<br \/>l<br \/>PS&gt; <strong>[string][char[]][int[]][char[]]&#8221;PowerShell&#8221;<br \/><\/strong>P o w e r S h e l l<br \/>PS&gt;<\/font><\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>PSMDTAG:FAQ: What are cascading type casts?<\/p>\n<p>PSMDTAG:DOTNET:&nbsp; [char[]]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A user on the newsgroup wondered whether the example below highlighted a bug or a feature.&nbsp; They applied multiple type declarations on a single parameter and Windows PowerShell did not complain. function check { &nbsp; param([string] [int] $y = 0, $x = 0)###^^^^^^^^^^^^&nbsp; $x.GetType().ToString();&nbsp; $y.GetType().ToString();&nbsp; $x + $y;} That&#8217;s no bug, that&#8217;s a feature.&nbsp; \ud83d\ude42 [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[14,10],"class_list":["post-10001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-dotnet","tag-faq"],"acf":[],"blog_post_summary":"<p>A user on the newsgroup wondered whether the example below highlighted a bug or a feature.&nbsp; They applied multiple type declarations on a single parameter and Windows PowerShell did not complain. function check { &nbsp; param([string] [int] $y = 0, $x = 0)###^^^^^^^^^^^^&nbsp; $x.GetType().ToString();&nbsp; $y.GetType().ToString();&nbsp; $x + $y;} That&#8217;s no bug, that&#8217;s a feature.&nbsp; \ud83d\ude42 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10001"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10001\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}