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
How to check if a C# list is empty?
In C#, there are several ways to check if a List is empty. The most common approaches include using the Count property, the Any() LINQ method, or comparing against an empty collection.
Syntax
Following are the common syntaxes for checking if a list is empty −
// Using Count property
if (list.Count == 0) { }
// Using Any() method
if (!list.Any()) { }
// Using Count with comparison
if (list.Count > 0) { }
Using Count Property
The Count property returns the number of elements in the list. When Count equals zero, the list is empty −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var subjects = new List<string>();
subjects.Add("Maths");
subjects.Add("Java");
subjects.Add("English");
subjects.Add("Science");
Console.WriteLine("List contents:");
foreach (var sub in subjects) {
Console.WriteLine(sub);
}
if (subjects.Count == 0) {
Console.WriteLine("List is empty");
} else {
Console.WriteLine("List is not empty. Count: " + subjects.Count);
}
// Test with empty list
var emptyList = new List<string>();
if (emptyList.Count == 0) {
Console.WriteLine("Empty list detected using Count");
}
}
}
The output of the above code is −
List contents: Maths Java English Science List is not empty. Count: 4 Empty list detected using Count
Using Any() Method
The Any() LINQ method returns true if the list contains any elements, false if empty −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main(string[] args) {
var subjects = new List<string>();
subjects.Add("Physics");
subjects.Add("Chemistry");
Console.WriteLine("List contents:");
foreach (var sub in subjects) {
Console.WriteLine(sub);
}
bool isEmpty = !subjects.Any();
if (isEmpty) {
Console.WriteLine("List is empty");
} else {
Console.WriteLine("List is not empty");
}
// Test with empty list
var emptyList = new List<string>();
if (!emptyList.Any()) {
Console.WriteLine("Empty list detected using Any()");
}
}
}
The output of the above code is −
List contents: Physics Chemistry List is not empty Empty list detected using Any()
Comparison of Methods
| Method | Performance | Readability | LINQ Required |
|---|---|---|---|
| Count == 0 | Fast - O(1) | Very clear | No |
| !Any() | Fast - O(1) | Expressive | Yes |
| Count > 0 | Fast - O(1) | Clear | No |
Checking Multiple Conditions
You can combine empty checks with null checks for safer code −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main(string[] args) {
List<string> nullList = null;
List<string> emptyList = new List<string>();
List<string> populatedList = new List<string> { "Item1", "Item2" };
CheckList(nullList, "Null List");
CheckList(emptyList, "Empty List");
CheckList(populatedList, "Populated List");
}
static void CheckList(List<string> list, string listName) {
Console.WriteLine($"Checking {listName}:");
if (list == null) {
Console.WriteLine(" List is null");
} else if (list.Count == 0) {
Console.WriteLine(" List is empty");
} else {
Console.WriteLine($" List has {list.Count} items");
}
Console.WriteLine();
}
}
The output of the above code is −
Checking Null List: List is null Checking Empty List: List is empty Checking Populated List: List has 2 items
Conclusion
To check if a C# list is empty, use list.Count == 0 for simplicity and performance, or !list.Any() for more expressive code. Both methods are efficient with O(1) complexity. Always consider null checks when working with lists that might be uninitialized.
