Pregunta

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?

¿Fue útil?

Solución

You can use null coalesce operation:

addParam("foo", Foo.Whatever ?? DBNull.Value);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top