问题
When I read a cell by doing Worksheets.Cells[2,5].value.ToString();
I get a error "System.NullReferenceException: Object reference not set to an instance of an object."
What would be a good way to check for null and then assign the value, without having to have a "if" statement.
回答1:
string strValue = Worksheets.Cells[2,5].value==null ? string.Empty : Worksheets.Cells[2,5].value.ToString();
or
object objValue = Worksheets.Cells[2,5].value ?? string.Empty
来源:https://stackoverflow.com/questions/11964271/reading-a-empty-cell-gives-object-reference-error