У меня есть коллекция целых чисел, допускающих значение NULL.
Почему компилятор позволяет переменной итерации иметь тип int not int? ?
List<int?> nullableInts = new List<int?>{1,2,3,null};
List<int> normalInts = new List<int>();
//Runtime exception when encounter null value
//Why not compilation exception?
foreach (int i in nullableInts)
{
//do sth
}
Конечно, я должен обращать внимание на то, что я повторяю, но было бы неплохо, если бы компилятор сделал мне выговор :) Как здесь:
foreach (bool i in collection)
{
// do sth
}
//Error 1 Cannot convert type 'int' to 'bool'