Display the default if element is not found in a C# List

When working with C# Lists, attempting to access an element that doesn't exist can throw exceptions. To safely handle empty lists or missing elements, you can use methods like FirstOrDefault() which return the default value for the type instead of throwing an exception.

For reference types like strings, the default value is null. For value types like int, float, or double, the default value is 0. For bool, it's false.

Syntax

Following is the syntax for using FirstOrDefault() on a List −

list.FirstOrDefault();
list.FirstOrDefault(condition);

You can also specify a custom default value using the overload −

list.FirstOrDefault(customDefaultValue);

Using FirstOrDefault() with Empty List

Example

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

class Demo {
    static void Main() {
        List<float> val = new List<float> { };
        float a = val.FirstOrDefault();
        Console.WriteLine("Default Value = " + a);
        
        if (a == 0.0f) {
            a = 0.1f;
        }
        Console.WriteLine("Default Float value updated = " + a);
    }
}

The output of the above code is −

Default Value = 0
Default Float value updated = 0.1

Using FirstOrDefault() with Different Data Types

Example

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

class Program {
    static void Main() {
        // Empty string list
        List<string> names = new List<string> { };
        string firstName = names.FirstOrDefault();
        Console.WriteLine("Default string: " + (firstName ?? "null"));
        
        // Empty integer list
        List<int> numbers = new List<int> { };
        int firstNumber = numbers.FirstOrDefault();
        Console.WriteLine("Default int: " + firstNumber);
        
        // Empty boolean list
        List<bool> flags = new List<bool> { };
        bool firstFlag = flags.FirstOrDefault();
        Console.WriteLine("Default bool: " + firstFlag);
    }
}

The output of the above code is −

Default string: null
Default int: 0
Default bool: False

Using FirstOrDefault() with Conditions

You can also use FirstOrDefault() with a condition to find the first element that matches specific criteria, returning the default value if no match is found −

Example

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

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 1, 3, 5, 7, 9 };
        
        // Find first even number (doesn't exist in this list)
        int firstEven = numbers.FirstOrDefault(x => x % 2 == 0);
        Console.WriteLine("First even number: " + firstEven);
        
        // Find first number greater than 5
        int firstGreater = numbers.FirstOrDefault(x => x > 5);
        Console.WriteLine("First number > 5: " + firstGreater);
    }
}

The output of the above code is −

First even number: 0
First number > 5: 7

Default Values by Data Type

Data Type Default Value
int, float, double, decimal 0
bool false
string, object, arrays null
char '\0' (null character)

Conclusion

The FirstOrDefault() method is essential for safe list operations in C#. It prevents exceptions when accessing empty lists or searching for non-existent elements by returning the appropriate default value for each data type instead of throwing an error.

Updated on: 2026-03-17T07:04:35+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements