Defaults for null values
Posted
by OldFart
on Stack Overflow
See other posts from Stack Overflow
or by OldFart
Published on 2010-03-05T21:48:36Z
Indexed on
2010/03/08
18:36 UTC
Read the original article
Hit count: 319
powershell
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 }
© Stack Overflow or respective owner