2
我有一個代碼:的NullReferenceException在長度
string[] sizes = new string[] {"1","11","2","22","200","222", null, "105", "101", "102", "103", "90" };
try
{
var size = from x in sizes
orderby x.Length, x
select x;
foreach (var p in size)
{
Console.WriteLine(p);
}
Console.Read();
}
catch (NullReferenceException)
{
}
我如何能趕上空在x.Length,並繼續執行代碼?
怎麼樣「其中x不爲空」條款或類似的東西? – Qnan 2012-08-10 10:27:42
其中x!= null – h1ghfive 2012-08-10 10:28:57
您將無法捕捉到異常並繼續從您所在的位置(您不應該)。堆棧展開將發生,您將不會在try和catch之間有範圍變量。像別人說的那樣去做,並通過檢查null來避免它。 – 2012-08-10 10:32:10