What is the convert.ToString equivalent of the variable.ToString() in this scenario?
they used variable.ToString() in this instance which makes sense, but say the value was null so you would have to use convert.ToString, so then how would u format Convert.ToString(variable) into currency or 3dp? Just need a bit of help with what the code would be. Thanks
9 Replies
i have never used Convert.ToString for anything
why not just nullcheck?
variable?.ToString("format") ?? "text if null"
I'm new to coding, does that just avoid the null exception?
yes
the error thing if its null
ah ok
?.
is conditional accessi see, thanks
and
??
is null coalescingit's shorthand for checking if the variable is null before accessing a member of it, and if it is it short-circuits the whole expression to return null
then
??
is "if the left hand side is null return the right hand side, otherwise return the left hand side"that makes sense, thanks guys :)