【发布时间】:2012-08-22 10:54:41
【问题描述】:
使用此代码:
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[] 的协变数组转换可能导致写入操作时出现运行时异常”
所以我让它进行它想要的更改(“将局部变量的类型更改为对象[] ...”),以便代码变成这样:
...
object[] currentRowContents = new string[ColumnCount];
...
然后,当我重新运行 Resharper 检查时,我再次从 Resharper 收到完全相同的警告消息(“从字符串 [] 到对象 [] 的协变数组转换可能导致写入操作时出现运行时异常”),不过这次就行了:
object[] currentRowContents = new string[ColumnCount];
然后我让它再做一次(“将局部变量的类型更改为字符串[]...”)
...所以这会将该行更改为:
string[] currentRowContents = new string[PLATYPUS_COL_COUNT];
...(IOW,它将其更改回第一个版本,但使用显式而不是隐式字符串数组声明);但随后运行 ReSharper 会希望我将 string[] 更改为 var 等。
【问题讨论】:
标签: c# arrays winforms resharper