What is comparison Operator?
Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.
Type comparison
The type comparison operators (-is and -isnot) are used to determine if an object is a specific type.
Operatorsย | Description |
---|---|
-is | Returns true if both object are the same type |
-isnot | Returns true if the objects are not the same type |
-is Operator
Syntax:ย <object> -is <type reference>
Example:
1 2 3 4 5 |
PS C:\> $stringMsg1 = "dotnet-helpers" $stringMsg2 = 100 $stringMsg1 -is $stringMsg2.GetType() $stringMsg2 -is [int] |
OUTPUT
1 2 |
False True |
-isnot Operator
Syntax:ย <object> -is <type reference>
Example:
1 2 3 4 5 |
PS C:\> $stringMsg1 = "dotnet-helpers" $stringMsg2 = 100 $stringMsg1 -isnot $stringMsg2.GetType() $stringMsg2 -isnot [int] |
OUTPUT
1 2 |
True False |
Hi David,
The left hand side value always be an object (-is of a type). please find the below syntax.
shouldn’t this return true? But I get false
“dfklj”.gettype() -is “lsdkjf”.gettype()