Preface: I'm starting to learn C# and I don't want to port any of my bad habits from other languages, so I'm following convention wherever possible.
Using the default Visual Studio code formatting, this relatively simple function requires 14 lines of code.
public void add(int[] values, Func<int, int> transform = null)
{
foreach (int v in values)
{
if (transform == null)
{
add(v);
}
else
{
add(transform(v));
}
}
}
My first thought was to use the ??
operator to do something like the following snippet, but apparently that's gibberish.
public void add(int[] values, Func<int, int> transform = null)
{
foreach (int v in values)
{
add(transform(v) ?? v);
}
}
Is there a more concise and/or readable way to write the following method?