c# - null / DBNull conversion -


this question has answer here:

i have method

void addparam(string name, object value); 

and object

public class foo  {    public string whatever; } 

what best way perform (working) call match logic?

addparam("foo", foo.whatever == null ? dbnull.value : foo.whatever); 

i thinking such thing this:

object getparamvalue(object value) {   if (value == null) return dbnull.value;   return value; }  addparam("foo", getparamvalue(valuefoo.whatever)); 

how can achieve behavior?

you can use null coalesce operation:

addparam("foo", foo.whatever ?? dbnull.value); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -