Index was outside the bounds of the array.
背景今天寫了這麼一個函數:
private string DateFormatConversion(string str) { string year = ""; char[] pstr = str.ToCharArray(); for (int k = 0; k < 4; k++) { year += pstr[k]; } return year; }
當str傳入值如空的時候,就會提示“Index was outside the bounds of the array.”錯誤。
解決方案:
在處理str之前,先判斷str.Length是否大於零
最後更新:2017-04-03 12:54:18