Makes the -eq keyword in a power shell check referential equality or uses Object.Equals ()

Does "-eq" use PowerShell checks in the equation (for example, "==" in C #) or does the equivalent call Object.Equals ()

+5
source share
2 answers

The equality test is not so simple.

Note that 'a' -eq 'A'returns true. This means that PowerShell does more than just invoke Equals.

Further for your objects it Equalsis caused as expected.

Add-Type -TypeDefinition @"
    using System;
    public class X {
        public string Property;
        public X(string s) {
            Property = s;
        }
        public override bool Equals(object o) {
            System.Console.WriteLine("equals on {0}", Property);
            return Property == ((X)o).Property;
        }
        public override int GetHashCode() { return 20; }
    }
"@

$o1 = New-Object X 'a'
$o2 = New-Object X 'A'

$o1 -eq $o2

, PowerShell . , , . '1' -eq 1 .

+5

-eq (, "==" #) Object.Equals(). : . , , , , .

# this is False
[ConsoleColor]::Black -eq $true

# this is True
$true -eq [ConsoleColor]::Black
+3

All Articles