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?

有帮助吗?

解决方案

You can use null coalesce operation:

addParam("foo", Foo.Whatever ?? DBNull.Value);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top