有了这个代码:如何避免这种潜在的“写操作运行时异常”?
for (int row = 0; row < PlatypusGridRowCount; row++) {
// Save each row as an array
var currentRowContents = new string[ColumnCount];
// Add each column to the currentColumn
for (int col = 0; col < ColumnCount; col++) {
currentRowContents[col] = this.GetPlatypusValForCell(row, col);
}
// Add the row to the DGV
dataGridViewPlatypus.Rows.Add(currentRowContents);
ReSharper的说,最后一行:“从string []联合变量数组转换为一个对象[]可能会导致在写入操作运行时异常”
所以我让它做就是了变化(“更改类型局部变量的反对[] ...”),这样的代码变得这样:
...
object[] currentRowContents = new string[ColumnCount];
...
然后,当我重新运行ReSharper的检查,我得到的从Resharper(“Co-va从string [] riant数组转换为一个对象[]可能会导致在写入操作运行时异常“),但这次上线:
object[] currentRowContents = new string[ColumnCount];
我再让它重新做它的事(”变型局部变量的String [] ...“)
...所以这改变了行:
string[] currentRowContents = new string[PLATYPUS_COL_COUNT];
...(IOW,它改变了它回到了第一个版本,但与显式而非隐式字符串数组声明);但ReSharper的的后续运行将要我换的String []为VAR等