If I use the [char]
datatype, it requires the value to be one character, e.x.
[char]"a"
a
Since if I use it with more than one character it will give me an error:
[char]"ab"
Cannot convert value "ab" to type "System.Char". Error: "String must be exactly one
character long."
At line:1 char:1
+ [char]"ab"
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocation
However if I use
[char[]]"ab"
I get the output
a
b
If I compare Get-Member
on both, I get no result:
PS C:\Users\nijoh> Compare-Object -ReferenceObject $([char] | gm) -DifferenceObject $([char[]] | gm) -PassThru
PS C:\Users\nijoh>
But I can see that they are two distinct types since they show up differently:
PS C:\Users\nijoh> ([char[]]"a").GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Char[] System.Array
PS C:\Users\nijoh> ([char]"a").GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Char System.ValueType
So what is the difference between the [char]
and [char[]]
datatypes in Powershell?