B. *_*non 4 c# arrays resharper winforms
使用此代码:
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);
Run Code Online (Sandbox Code Playgroud)
Resharper谈到最后一行:"从字符串[]到对象[]的共变量数组转换会导致写入操作的运行时异常"
所以我让它进行它想要的更改("将局部变量的类型更改为object [] ..."),以便代码变为:
...
object[] currentRowContents = new string[ColumnCount];
...
Run Code Online (Sandbox Code Playgroud)
然后,当我重新运行Resharper检查时,我再次从Resharper获得完全相同的确切警告消息("从字符串[]到对象[]的共变量数组转换可能导致写入操作的运行时异常"),但这次在线上:
object[] currentRowContents = new string[ColumnCount];
Run Code Online (Sandbox Code Playgroud)
然后我让它再做一次("将局部变量的类型改为string [] ...")
...所以这会将该行更改为:
string[] currentRowContents = new string[PLATYPUS_COL_COUNT];
Run Code Online (Sandbox Code Playgroud)
...(IOW,它将其更改回第一个版本,但使用显式而非隐式字符串数组声明); 但随后的ReSharper运行会要求我将string []更改为var等.
当数组用于从中检索数据时,您的代码绝对完美.
不幸的是,ReSharper无法检测是否有对该数组的写入(全局程序验证目前在计算机科学中尚未解决问题:)),如果有数组元素的赋值,程序将在运行时抛出异常,如果除了字符串之外的东西被赋予.