0

i have a method

void addParam(string name, object value);

and an object

public class Foo 
{
   public string Whatever;
}

what is the best way to perform a (working) call that would match this logic?

addParam("foo", Foo.Whatever == null ? DBNull.Value : Foo.Whatever);

I was thinking about such a thing like this:

object getParamValue(object value)
{
  if (value == null) return DBNull.Value;
  return value;
}

addParam("foo", getParamValue(ValueFoo.Whatever));

How can i achieve this behavior?

1 Answers1

0

You can use null coalesce operation:

addParam("foo", Foo.Whatever ?? DBNull.Value);
Ortiga
  • 8,455
  • 5
  • 42
  • 71