Open In App

LINQ | Generation Operator | DefaultIfEmpty

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.

  • This operator is overloaded in two different ways:
    • DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource): This method is used to return the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.
    • DefaultIfEmpty<TSource>(IEnumerable<TSource>): This method is used to return the elements of the specified sequence or the type parameter’s default value in a singleton collection if the sequence is empty.
  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • It is implemented by using deferred execution.
  • DefaultIfEmpty<TSource>(IEnumerable<TSource>) will return ArgumentNullException if the given source is null.
  • The default value for the reference types and for the nullable types is null.

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


Last Updated : 26 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads