質問

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