我注意到我编写的一个方法提前退出,并且在尝试访问集合中的第一个元素(如 string[] 或 List)时,如果它为空,则不会引发任何异常。例如
var emptyList = new List<string>();
var aha = emptyList.Where(i => i == "four");
var props = aha.First();
//anything after here in the same method does not run
这是正确的,这怎么可能是编译器中的一个有用功能?!(使用.Net 4)
编辑完整的winforms程序:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var emptyList = new List<string>();
var aha = emptyList.Where(i => i == "four");
var props = aha.First(); //throws exception
var fdsfsa = 0;
}
private void useref() {
var emptyList = new List<string>();
var aha = emptyList.Where(i => i == "four");
var props = aha.First(); //exits method, doesn't throw exception?
var asdf = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
useref();
}
}