Pawan Mishra
Pawan Mishra

Reputation: 7268

PowerShell and $null as method parameter

Is PowerShell's $null equivalent to .NET's null? I have a scenario, wherein I have to invoke a .NET method from PowerShell and pass null as method parameter. As of now, I am getting some assembly loading related error on method invocation. I am wondering if $null is the culprit. Also, please share if you know what is the correct way of invoking .NET method with null parameter from PowerShell.

Method signature:

void SetUp(IKernel kernel, Action<IBindingInSyntax<object>> obj)

Thanks

Upvotes: 2

Views: 3384

Answers (1)

Serjx86
Serjx86

Reputation: 530

PowerShell converts it's $null to .NET's null just fine in function calls.

Proof by example.

Add-Type -TypeDefinition @"
public static class Foo
{
    public static bool IsNull(object o)
    {
        return o == null;
    }
}
"@ -Language CSharp

[Foo]::IsNull($null)

[Foo]::IsNull("string")

Output:

True
False

$null wraps a null reference to an object, and no wonder it just works, even though they are two different things.

Upvotes: 2

Related Questions