TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DefaultIfEmpty Method

This C# tutorial shows how to use the DefaultIfEmpty extension method from System.Linq.

DefaultIfEmpty handles empty collections in a special way.

It returns a default value, not an error. With it, we replace empty collections with a singleton collection that may be more compatible with other parts of the C# program.

Example. The concept of DefaultIfEmpty is simple: it replaces an empty collection with a collection of one default value. Remember that the default value of int is 0. Thus, DefaultIfEmpty on a List<int> yields a List with one zero element.

C# program that uses DefaultIfEmpty

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
	// Empty list.
	List<int> list = new List<int>();
	var result = list.DefaultIfEmpty();

	// One element in collection with default(int) value.
	foreach (var value in result)
	{
	    Console.WriteLine(value);
	}

	result = list.DefaultIfEmpty(-1);

	// One element in collection with -1 value.
	foreach (var value in result)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

0
-1

DefaultIfEmpty with argument. You can pass an argument to DefaultIfEmpty. This will become the value that is used in the singleton collection's value. Please see the next section for discussion about this functionality.

Usage. If you call DefaultIfEmpty on a null reference, you will get a NullReferenceException. DefaultIfEmpty can only be used to handle empty collections. It is useful if in other locations in your program, empty collections cannot be used.

Null TipsNullReferenceException and Null Parameter

The version that receives an argument may be more useful. You can use a custom default object that has special behavior. In the book Refactoring, this technique is referred to as a null object (see Introduce Null Object).

Tip: You could pass a reference to the null object to DefaultIfEmpty. Then methods can act without knowing about empty collections.

Summary. We saw the behavior of the DefaultIfEmpty method on an empty List collection. The method changes empty collections to collections with one element. With the optional parameter, you can use custom default objects.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf