简体   繁体   English

[数据类型]和[数据类型[]]之间的区别

[英]Difference between [datatype] and [datatype[]]

When casting a datatype for a variable, what does the extra empty square brackets do? 在为变量转换数据类型时,多余的空方括号有什么作用?

For example, in defining a parameter for a function, I have noticed these two ways of casting a data type for that parameter: 例如,在为函数定义参数时,我注意到了以下两种为该参数转换数据类型的方式:

param(
    [string]$computername
)

vs: vs:

param(
    [string[]]$computername
)

What purpose does the empty square brackets in the second example serve? 第二个示例中的空方括号起什么作用?

The parameter in the second example accepts a list of strings (an array), the parameter in the first example only a single string. 第二个示例中的参数接受字符串列表(数组),第一个示例中的参数仅单个字符串。

This works in both cases: 这在两种情况下均有效:

-computername 'foo'

This works just in the second case: 仅在第二种情况下有效:

-computername 'foo', 'bar'

[] denotes an array. []表示一个数组。 So [string[]]$computername would denote a string array of computername's. 因此[string[]]$computername将表示计算机名的字符串数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM