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
Null List in C#
A null list in C# refers to a List reference that points to nothing instead of an actual List object. This is different from an empty list, which is an initialized List with zero elements. Understanding null lists is crucial for avoiding NullReferenceException errors in your applications.
Syntax
To declare a null list −
List<T> listName = null;
To check if a list is null −
if (listName == null) {
// handle null case
}
Creating and Checking Null Lists
When you declare a List variable without initialization, it defaults to null. You can explicitly assign null or check for null using the equality operator −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> myList = null;
// checking for null
Console.WriteLine("Is myList null? " + (myList == null));
// This would throw NullReferenceException
// Console.WriteLine(myList.Count);
// Safe way to check
if (myList == null) {
Console.WriteLine("List is null, cannot access properties");
}
}
}
The output of the above code is −
Is myList null? True List is null, cannot access properties
Null List vs Empty List
It's important to distinguish between a null list and an empty list −
Example
using System;
using System.Collections.Generic;
public class ListComparison {
public static void Main() {
// Null list
List<int> nullList = null;
// Empty list
List<int> emptyList = new List<int>();
Console.WriteLine("Null list check: " + (nullList == null));
Console.WriteLine("Empty list check: " + (emptyList == null));
Console.WriteLine("Empty list count: " + emptyList.Count);
// Safe null checking
Console.WriteLine("Null list count: " + (nullList?.Count ?? -1));
}
}
The output of the above code is −
Null list check: True Empty list check: False Empty list count: 0 Null list count: -1
Safe Null Checking Methods
C# provides several ways to safely handle null lists −
Using Null-Conditional Operator
using System;
using System.Collections.Generic;
public class SafeNullCheck {
public static void Main() {
List<string> names = null;
// Safe count check
int count = names?.Count ?? 0;
Console.WriteLine("Safe count: " + count);
// Safe method call
bool hasItems = names?.Any() ?? false;
Console.WriteLine("Has items: " + hasItems);
// Initialize if null
names ??= new List<string>();
names.Add("Alice");
Console.WriteLine("After initialization: " + names.Count);
}
}
The output of the above code is −
Safe count: 0 Has items: False After initialization: 1
Common Use Cases
| Scenario | Check Method | Purpose |
|---|---|---|
| Method parameter validation | if (list == null) |
Prevent NullReferenceException |
| Safe property access | list?.Count |
Return null if list is null |
| Initialize if null | list ??= new List<T>() |
Lazy initialization |
| Default value handling | list?.Count ?? 0 |
Provide fallback value |
Conclusion
Null lists in C# are references that point to nothing, unlike empty lists which are initialized objects with zero elements. Always check for null before accessing list properties or methods to avoid NullReferenceException, and use null-conditional operators for safer code.
