如何在列表前放一个逗号影响其类型?
看看下面的代码:
function StartProgram
{
    $firstList = getListMethodOne
    Write-Host "firstList is of type $($firstList.gettype())"
    $secondList = getListMethodTwo
    Write-Host "secondList is of type $($secondList.gettype())"
}
function getListMethodOne
{
    $list = new-object system.collections.generic.list[string]
    $list.Add("foo") #If there is one element, $list is of type String
    $list.Add("bar") #If there is more than one element, $list is of type System.Object[]
    return $list 
}
function getListMethodTwo
{
    $list = new-object system.collections.generic.list[string]
    $list.Add("foo")
    $list.Add("bar")
    return ,$list #This is always of type List[string]
}
StartProgram
为什么呢,如果你不返回之前使用逗号$list在getListMethodOne它返回类型System.Object[],而如果你在做使用逗号getListMethodTwo,它的类型List[string]如预期?
PS:我正在使用PSVersion 4.0
| 归档时间: | 
 | 
| 查看次数: | 1861 次 | 
| 最近记录: |