

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to check if a C# list is empty?
Use the Any method to find whether the list is empty or not.
Set the list −
var subjects = new List<string>(); subjects.Add("Maths"); subjects.Add("Java"); subjects.Add("English"); subjects.Add("Science"); subjects.Add("Physics"); subjects.Add("Chemistry");
Now set the following condition to check whether the list is empty or not −
bool isEmpty = !subjects.Any(); if(isEmpty) { Console.WriteLine("Empty"); }else { Console.WriteLine("List is not empty"); }
The following is the complete code −
Example
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("Maths"); subjects.Add("Java"); subjects.Add("English"); subjects.Add("Science"); subjects.Add("Physics"); subjects.Add("Chemistry"); foreach (var sub in subjects) { Console.WriteLine(sub); } bool isEmpty = !subjects.Any(); if(isEmpty) { Console.WriteLine("Empty"); } else { Console.WriteLine("List is not empty"); } } }
- Related Questions & Answers
- How to check if a list is empty in Python?
- Check if a list is not empty in MongoDB?
- What is best way to check if a list is empty in Python?
- How to check if android editText is empty?
- How to check if a string is empty in Kotlin?
- C# program to check whether a list is empty or not
- Python - Check if dictionary is empty
- How to check if String is empty in Java?
- How to check if PSCustomObject is empty in PowerShell?
- Check if a HashMap is empty in Java
- How to empty a C# list?
- How to Check if Android EditText is empty in Kotlin?
- How to check if the file is empty using PowerShell?
- Check if value is empty in JavaScript
- Check if a directory is not empty in Java
Advertisements