Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# DefaultIfEmpty Method
The DefaultIfEmpty method in C# is a LINQ extension method used to handle empty collections gracefully. Instead of returning an empty sequence that might cause issues in further operations, it returns a sequence containing a single default value when the original collection is empty.
Syntax
Following is the syntax for the DefaultIfEmpty method −
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
this IEnumerable<TSource> source
)
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
this IEnumerable<TSource> source,
TSource defaultValue
)
Parameters
- source − The sequence to return a default value for if it is empty.
- defaultValue − The value to return if the sequence is empty. If not specified, uses the default value for the type.
Return Value
Returns an IEnumerable<T> that contains defaultValue if source is empty; otherwise, returns the original source sequence.
Using DefaultIfEmpty with Empty Collections
When a collection is empty, DefaultIfEmpty returns the default value for the data type −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<double> myList = new List<double>();
var res = myList.DefaultIfEmpty();
Console.WriteLine("Empty list with DefaultIfEmpty:");
foreach (var a in res) {
Console.WriteLine(a);
}
// With integers
List<int> emptyInts = new List<int>();
var intResult = emptyInts.DefaultIfEmpty();
Console.WriteLine("\nEmpty int list:");
foreach (var num in intResult) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Empty list with DefaultIfEmpty: 0 Empty int list: 0
Using DefaultIfEmpty with Custom Default Values
You can specify a custom default value to be returned when the collection is empty −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<string> emptyNames = new List<string>();
var namesResult = emptyNames.DefaultIfEmpty("No Name Available");
Console.WriteLine("Empty string list with custom default:");
foreach (var name in namesResult) {
Console.WriteLine(name);
}
List<int> emptyNumbers = new List<int>();
var numbersResult = emptyNumbers.DefaultIfEmpty(-1);
Console.WriteLine("\nEmpty int list with custom default:");
foreach (var num in numbersResult) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Empty string list with custom default: No Name Available Empty int list with custom default: -1
Using DefaultIfEmpty with Non-Empty Collections
When the collection contains elements, DefaultIfEmpty returns the original collection unchanged −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30 };
var result = numbers.DefaultIfEmpty(999);
Console.WriteLine("Non-empty list with DefaultIfEmpty:");
foreach (var num in result) {
Console.WriteLine(num);
}
// Empty list for comparison
List<int> emptyList = new List<int>();
var emptyResult = emptyList.DefaultIfEmpty(999);
Console.WriteLine("\nEmpty list with DefaultIfEmpty:");
foreach (var num in emptyResult) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Non-empty list with DefaultIfEmpty: 10 20 30 Empty list with DefaultIfEmpty: 999
Common Use Cases
DefaultIfEmpty is particularly useful in LINQ queries where you want to ensure that operations like joins or aggregations don't fail on empty collections. It's commonly used with SelectMany and group joins to handle scenarios where related data might not exist.
Conclusion
The DefaultIfEmpty method provides a safe way to handle empty collections by returning a default value instead of an empty sequence. It accepts an optional parameter to specify a custom default value, making it flexible for various scenarios where you need to ensure your collections always contain at least one element.
