powershell - 如何在Powershell中创建可变长度的char数组?

标签 powershell

如何在Powershell中创建长度为$ Count的char数组?

PS C:\Users\Administrator> $Count
415
PS C:\Users\Administrator> $chars = New-Object System.Char ($Count)
New-Object : Constructor not found. Cannot find an appropriate constructor for type System.Char.
At line:1 char:10
+ $chars = New-Object System.Char ($Count)
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

I tried 
[Char[]] $chars = @(415)

我有一个需要char数组的类,但是基本上我通过使用String找到了解决问题的方法。但是我只是想问问是否有人知道如何创建一个可变长度的空char数组。

例如:如何在Powershell中执行此操作? C#-> var chars = new Char [Count];

最佳答案

在PowerShell 5.0中,可以使用 new() constructor method:

PS C:\> $Count = 415
PS C:\> $chars = [char[]]::new($Count)
PS C:\> $chars.Count
415

在早期版本中,请使用 New-Object cmdlet,并指示您想要一个带有[]的数组:
$chars = New-Object -TypeName 'char[]' -ArgumentList $Count

关于powershell - 如何在Powershell中创建可变长度的char数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639991/

相关文章:

powershell - 如何对选择字符串提取的数字求和?

powershell - 如何在字符串中转义变量,PowerShell

powershell - 我可以在Windows XP SP2上使用哪些免费的Powershell编辑器?

powershell - 如果未设置 TFS 构建变量,如何回退到默认值?

c# - 您会推荐 Iron Ruby、Iron Python 或 PowerShell 来使 C# 应用程序成为脚本宿主吗?

powershell - 如果设置了数据类型,参数值不能设置为空

powershell - 如何通过注册表中的属性子属性的值过滤结果

powershell - 测试路径返回 True 但 ExtractToDirectory 找不到路径

powershell - 获取长路径名上的文件夹 NTFS ACL

powershell - 在哪里下载Windows PowerShell v2(2.0)RTM?