Question

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?

Was it helpful?

Solution

You can use null coalesce operation:

addParam("foo", Foo.Whatever ?? DBNull.Value);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top