Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# DefaultIfEmpty Method
This method is used to handle empty collections. Instead of showing an error, this method displays a default value.
We have the following list.
List<double> myList = new List<double>();
As you can see, since the above list is empty, we can display the default value.
var res = myList.DefaultIfEmpty();
Let us see an example.
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<double> myList = new List<double>();
var res = myList.DefaultIfEmpty();
foreach (var a in res) {
Console.WriteLine(a);
}
}
}
Output
0
Advertisements