Esiste un modo per restituire facoltativamente un valore null con un iteratore "return yield" guidato?Ritorno rendimento con Null
Vorrei restituire un valore null in alcuni casi e non penso che questo sia specifico per IEnumerable di tipo stringa. Lo stesso vale per IEnumerable di tipo int ecc Grazie
static void Main(string[] args)
{
var Items = GetItems();
if (Items != null)
{
foreach (var item in Items)
{
Console.WriteLine(item);
}
}
else
{
Console.WriteLine("<null>");
}
}
static IEnumerable<string> GetItems()
{
if (false)
{
yield return "Andy";
yield return "Jennifer";
}
return null; // <- Compiler Error:
// Cannot return a value from an iterator.
// Use the yield return statement to return a value,
// or yield break to end the iteration.
}
la risposta a quello che ho chiesto, ma probabilmente mquander postato un approccio più corretto. – andleer