Defaults for null values
- by OldFart
Working on a Powershell script I had several places where I wanted A unless it was null, else B. Essentially the ?? operator in C#. I ended up writing the function shown below, but I can't help but think there is a built-in way to do this.
Is there a better, built-in, way?
function Get-ValueOrDefault()
{
foreach ($value in $args)
{
if ($value -ne $null) { return $value }
}
}
I think this works better:
function Get-ValueOrDefault() { $args | select -first 1 }