How to use List.DefaultIfEmpty() to replace an empty list with a list of one default value in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            var d = list.DefaultIfEmpty();

            foreach (var n in d)
                Console.WriteLine(n);
        }
    }
}


/*
run:
     
0
 
*/

 



answered Feb 21, 2017 by avibootz
0 votes
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            var d = list.DefaultIfEmpty(999);

            foreach (var n in d)
                Console.WriteLine(n);
        }
    }
}


/*
run:
     
999
 
*/

 



answered Feb 21, 2017 by avibootz

Related questions

1 answer 173 views
173 views asked Aug 22, 2018 by avibootz
1 answer 142 views
1 answer 242 views
1 answer 89 views
2 answers 247 views
...