看着this link given above我試過幾個例子。
根據這個定義填充的位置:在第三次調用這裏
public static void Fill(object[] array, int index, int count, object value)
{
for (int i = index; i < index + count; i++)
{
array[i] = value;
}
}
一個確實能夠得到一個ArrayTypeMismatchException
:
string[] strings = new string[100];
Fill(strings, 0, 100, "Undefined");
Fill(strings, 0, 10, null);
Fill(strings, 90, 10, 0); // a boxed 0 is not a string.
但是,如果我定義填寫這樣: 公共靜態無效FillT(T [] array,int index,int count,T value) {int i = index; i < index + count; i ++) { array [i] = value; }}
我不得到所述異常,因爲下面的行甚至不編譯
FillT(strings, 90, 10, 0);
沒有做到這一點
FillT<string>(strings, 90, 10, 0);
因此,在這種情況下,編譯器知道第三個參數必須是字符串類型,並且int不能在那裏傳遞。似乎使用泛型有助於避免這種情況下的令人討厭的錯誤。
來源
2013-11-01 14:11:58
PMF
我覺得文檔不對,它一定是'ctrl + c','ctrl + v'的問題。我希望這是建立在[二進制搜索]的非通用版本上(http://msdn.microsoft.com/en-us/library/ksz8za9f%28v=vs.100%29.aspx) –
查看已反編譯的源代碼(4.5),我只看到'if(array.Length - index
Kobi
你的問題不是捕捉異常 - 而是關於異常是否可以拋出。 –