我在會議上由這樣的session中獲取&分配值存儲多個值空值:如何處理,同時從會話列表檢索值
var imageSessList = (List<string>)Session["ImagesNames"];
if (imageSessList != null)
{
string image1 = imageSessList[0];
string image2 = imageSessList[1];
string image3 = imageSessList[2];
string image4 = imageSessList[3];
}
但如果會話只包含3個值,所以在分配string image4 = imageSessList[3];
時會拋出空錯誤。
如何在這種情況下處理null。
首先檢查列表的長度。它也會拋出IndexOutOfRange異常,而不是null異常。試圖關閉作爲重複的https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f – Rob