Am refactoring some old code and see something in the following format:
Type = rec["IsFlagged"]?.ToString() == "True" ? "Yes" : "No"
which should not work if rec["IsFlagged"] is null, in which case I need to return null. How would one refactor to keep code in one line, if possible?
I can do something like but am wondering if the following can be achieved in one line to have minimal changes to code
if (rec["IsFlagged"] != null)
{
return rec["IsFlagged"].ToString() == "True" ? "Yes" : "No"
}
else
{
return null
}
Thanks,
null == "True"would be false, since null is not equal to a string "True", so should result in "No"rec["IsFlagged"]is null code continues)... Make sure what question is asking is what you want (probably the case because second code is not equivalent the first one)rec["IsFlagged"]is aboolvalue when it's notnull. Can you confirm that?