Nullable types

In some rare cases, it may be desirable for a parameter to accept either null or a specific value type. For example, a parameter may need to accept either a date, or null.

A nullable type can be defined for the parameter:

function Test-Nullable {
param (
[Nullable[DateTime]]$Date
)
}

The function can be called with a null value for Date:

Test-Nullable -Date $null

Without the nullable type, a type-conversion error message would be thrown.

Not everything is nullable

System.String is not a nullable type. The documentation for Nullable explains why:

https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1?view=netframework-4.7.2.

In PowerShell, this is difficult to see. Assigning null to a variable with a String type will result in an empty string (not null). The result of the following statements is false:

[String]$String = $null
$null -eq $String
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.84.150