By Using DefaultIfEmpty() extension method you can check the sequence for empty and return something for it and do some manipulation on it afterwards.
By Default if you want to return some value other then 0 it will return that as well
For example
var numbers = new int[] {1,2,3,4,5,6,7}; Console.WriteLine(numbers.DefaultIfEmpty().Sum());If the sequence is empty it will return 0 otherwise it will sum and return that.
By Default if you want to return some value other then 0 it will return that as well
Console.WriteLine(numbers.DefaultIfEmpty(100).Sum());In this case it will return 100 if the sequence is empty.
Comments
Post a Comment