Open In App

LINQ | Generation Operator | DefaultIfEmpty

The generation operators are used for creating a new sequence of values. The Standard Query Operator supports 4 different types of generation operators:

  1. DefaultIfEmpty
  2. Empty
  3. Range
  4. Repeat

DefaultIfEmpty Operator

The DefaultIfEmpty operator is used to replace an empty collection or sequence with a default valued singleton collection or sequence. Or in other words, it returns a collection or sequence with default values if the source is empty, otherwise return the source.



Example 1:




// C# program to illustrate the
// use of DefaultIfEmpty operator
using System;
using System.Linq;
using System.Collections.Generic;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source 1
        int[] sequence1 = {};
  
        // The sequence is empty so it 
        // will return the default value
        // Using DefaultIfEmpty
        foreach(var val1 in sequence1.DefaultIfEmpty())
        {
            Console.WriteLine(val1);
        }
  
        // Data source 2
        string[] sequence2 = {"Geek", "Geeks123",
                                "GeeksforGeeks"};
  
        // The given sequence 2 is non-empty so 
        // it will return the sequence
        // Using DefaultIfEmpty
        foreach(var val2 in sequence2.DefaultIfEmpty())
        {
            Console.WriteLine(val2);
        }
    }
}

Output:
0
Geek
Geeks123
GeeksforGeeks

Example 2:




// C# program to illustrate the 
// use of DefaultIfEmpty operator
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Using DefaultIfEmpty operator
        foreach(Employee e in emp.DefaultIfEmpty())
        {
            Console.WriteLine(e.emp_name);
        }
    }
}

Output:

Anjita
Soniya
Rohit
Supriya
Anil
Anju

Article Tags :
C#