I noticed a method I wrote exits early and doesn't throw any exception when trying to access the first element in a collection (like string[] or List) if it is empty. E.g.
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
Is this correct, how can that be a useful feature in the compiler?! (using .Net 4)
Edit full winforms program:
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();
}
}