方法Array.SetValue(Object value,int index)允许使用常用索引器语法通常允许的涉及值/数组类型对的赋值,并且在尝试使用通用索引器语法时会引发异常结合通常不被允许的类型。例如,考虑下面的局部变量声明:将字节值赋给char数组
int[] twoints = new int[2] { 5, 6 };
下面四行不抛出任何运行时或编译时异常:
twoints[1] = (sbyte)7;
twoints.SetValue((sbyte)7, 1);
twoints[1] = (char)7;
twoints.SetValue((char)7, 1);
在另一方面,这四个的行会抛出一个异常,无论是在运行时或在设计时:
twoints[1] = 4.5;
twoints.SetValue(4.5, 1);
twoints[1] = 4L;
twoints.SetValue(4L, 1);
然而,当我一个字节的值赋给一个字符数组,我得到了奇怪的结果。索引器的语法在编译时被拒绝,并且的SetValue的API调用成功,在运行时:
char[] twochars = new char[2] { 'A', 'B' };
twochars[1] = (byte)70; // Not OK, refused by the compiler
twochars.SetValue((byte)70, 1); // OK, no exception at run-time
这是为什么呢操作允许?