我有两种相同功能的方法 - 一种是"if"条件,另一种是"??" 和铸造".哪种方法更好?为什么?
码:
Int16? reportID2 = null;
//Other code
//Approach 1
if (reportID2 == null)
{
command.Parameters.AddWithValue("@report_type_code", DBNull.Value);
}
else
{
command.Parameters.AddWithValue("@report_type_code", reportID2);
}
//Approach 2
command.Parameters.AddWithValue("@report_type_code", ((object) reportID2) ?? DBNull.Value);
Run Code Online (Sandbox Code Playgroud)
UPDATE
根据答案,以下是的好处 ??
注意:作为对象的铸造成本可以忽略不计.
参考