The shortest (IMHO) is:
int stockvalue = (reader["StockValue"] as int?) ?? 0;
Explanation:
- If reader["StockValue"] is of type int, the value will be returned, and the "??" operator will return the result
- If reader["StockValue"] is NOT of type int (e.g. DBNull), null will be returned, and the "??" operator will return the value 0 (zero).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…